简体   繁体   English

Laravel 8 API REST 登录

[英]Laravel 8 API REST Login

Hi there guys I'm having a little trouble making API with Laravel 8 it seems like my auth()->attempt don't understand my $data here's the part of my code that I'm doing, I'm not using the table Users, I'm using the table Usuarios, it's already set in providers that it should see my class Usuario can you see what I'm not seeing?大家好,我在使用 Laravel 8 制作 API 时遇到了一些麻烦,似乎我的 auth()->attempt 不理解我的 $data 这是我正在执行的代码部分,我没有使用表用户,我正在使用表 Usuario,它已经在提供者中设置,它应该看到我的类 Usuario 你能看到我没有看到的吗?

public function registrar(Request $request)
{
    $this->validate($request, [
        'nome' => 'required|min:4',
        'email' => 'required|email',
        'senha' => 'required|min:8',
        'telefone' => 'required|min:8'
    ]);

    $user = Usuario::create([
        'nome' => $request->nome,
        'email' => $request->email,
        'senha' => Hash::make($request->senha),
        'telefone' => $request->telefone
    ]);

    $token = $user->createToken('LaravelAuthApp')->accessToken;

    return response()->json(['token' => $token], 200);
}

/**
 * Login Req
 */
public function login(Request $request)
{
    $data = [
        'email' => $request->email,
        'senha' => $request->senha
    ];
    //dd($data);
    //$credentials = request('email','senha');
    dd(Auth::attempt($data));
    if (!Auth::attempt($data)) {
        return response()->json(['error' => 'Unauthorised'], 401);
    } else {
        $token = auth()->user()->createToken('LaravelAuthApp')->accessToken;
        return response()->json(['token' => $token], 200);
    }
}

And here it is my model if it's helpful如果有帮助,这是我的模型

<?php

      namespace App\Models;

      use Illuminate\Contracts\Auth\MustVerifyEmail;
      use Illuminate\Database\Eloquent\Factories\HasFactory;
      use Illuminate\Foundation\Auth\User as Authenticatable;
      use Illuminate\Notifications\Notifiable;
      use Laravel\Passport\HasApiTokens;

      class Usuario extends Authenticatable
     {
    use HasFactory, Notifiable, HasApiTokens;

    const TIPO = ['adm','usuario'];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    
    protected $fillable = [
        'nome',
        'tipo',
        'email',
        'senha',
        'telefone',
        'imagem',
        'cpf',
        'rg',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'senha',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verificado' => 'datetime',
    ];

    public function membro(){
        return $this->hasMany(Membro::class);
    }

    public function ingresso(){
        return $this->hasMany(Ingresso::class);
    }
}

try this尝试这个

 $data = [
        'email' => $request->email,
        'password' => $request->senha
    ];

as attempt() function look for email and password key作为attempt()函数查找emailpassword密钥

and change the database password并更改数据库password

Or或者

you can manually login via auth()->login($user);您可以通过auth()->login($user);手动登录auth()->login($user); function功能

$user = Usuario::where('email',$request->email)->first();
if(!$user){
    return response()->json(['error' => 'user not found.'], 401);
}

if (!Hash::check($request->senha,$user->senha)) {
    return response()->json(['error' => 'Unauthorised'], 401);
} else {
    auth()->login($user);
    $token = auth()->user()->createToken('LaravelAuthApp')->accessToken;
    return response()->json(['token' => $token], 200);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM