Last active
February 4, 2020 07:20
-
-
Save godilite/4cb63c9ec25490ccda1fa1795a2b81d2 to your computer and use it in GitHub Desktop.
Login Method for Laravel API with passport Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function login() | |
{ | |
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) { | |
$user = Auth::user(); | |
$success['token'] = $user->createToken('appToken')->accessToken; | |
//After successfull authentication, notice how I return json parameters | |
return response()->json([ | |
'success' => true, | |
'token' => $success, | |
'user' => $user | |
]); | |
} else { | |
//if authentication is unsuccessfull, notice how I return json parameters | |
return response()->json([ | |
'success' => false, | |
'message' => 'Invalid Email or Password', | |
], 401); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment