Skip to content

Instantly share code, notes, and snippets.

@godilite
Last active February 4, 2020 07:20
Show Gist options
  • Save godilite/4cb63c9ec25490ccda1fa1795a2b81d2 to your computer and use it in GitHub Desktop.
Save godilite/4cb63c9ec25490ccda1fa1795a2b81d2 to your computer and use it in GitHub Desktop.
Login Method for Laravel API with passport Authentication
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