简体   繁体   中英

Lumen 5.8: Implement login

I'm new to Lumen. How can I implement login?

I've tried this code but I got an error. And I found out in the documentation that Lumen does not support session. So Auth::attempt() is not available.

public function login(Request $request)
{
        $this->validate($request, [
            'email' => 'required|string|email',
            'password' => 'required|string'
        ]);

        $credentials = $request->only('email', 'password');
        if( !Auth::attempt($credentials) ) {
             return response()->json([
                 'message' => 'Unauthorized'
             ], 401);
        }

        return response()->json(['message' => 'Successfully login'], 200);
}

How can I authenticate user using login method? Since Auth::attempt() is not working are there any alternatives? Thanks!

Take email and password and check in the database manually.

    $email = $request->input('email');
    $password = $request->input('password');
    $result = DB::table('users')->where('email', $email)->first();
    if (!is_null($result)) {
        if($password == $result->password) {
            return response(200);
        }
} 

Checking in table users, in columns email and password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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