Search code examples
phplaravellumen

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!


Solution

  • 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.