dimanche 29 mai 2022

How to do login using angular and laravel api?

login.component.html

<form class="form-horizontal" #loginForm="ngForm" (ngSubmit)="onSubmit(loginForm)">
    <fieldset>
        <div class="control-group">
            <label class="control-label" for="email">E-mail</label>
            <div class="controls">
                <input type="text" id="email" name="email" placeholder="" class="input-xlarge" ngModel>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="password">Password</label>
            <div class="controls">
                <input type="password" id="password" name="password" placeholder="" class="input-xlarge" ngModel>
            </div>
        </div>

        <div class="control-group">
            <div class="controls">
                <button class="btn btn-success">Login</button>
            </div>
        </div>
    </fieldset>
</form>

login.component.ts

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

    constructor(private http:HttpClient) { }

    ngOnInit(): void {
    }

    onSubmit(form:NgForm){
        const email = form.value.email;
        const password = form.value.password;
        this.http.post("http://127.0.0.1:8000/api/signin",{
            email: email,
            password:password
        }).subscribe((res)=>{
            console.log(res);
        },
        err=>{
            console.log(err);
        });
    }
}

api.php

Route::post('/signin', 'AdminController@signin')->name("signin");

AdminController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use DB;

class AdminController extends Controller
{
    public function signin(Request $request)
    {
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) 
        {
            return response(['message'=>'login success!!'], 200);
        }
        else
        {
            return response(['message'=>'Login details are invalid'], 200);
        }
    }
}

I am new in angular and In above code I am simply want to login using angular and laravel web API. What happen here I am successfully pass parameter using angular and also successfully got in AdminContoller function i.e. signin function when I try to print to check the value of email and password then it show successfully but when I try to login via email and password then it always show Login details are invalid message. I don't know where am I doing wrong? Please help me.

Thank You



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/NIcRQbE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire