public function login($data) { $credentials = ['email' => strtolower(array_get($data, 'email')), 'password' => array_get($data, 'password')]; if (!($token = $this->returnToken($credentials))) { throw new Exception('No user with that email and password combination exists in our database.', 500); } return [JWTAuth::authenticate($token), $token]; }
/** * Authenticate the user using JWT. * * @param AuthJwtRequest $request * @return $this|\Illuminate\Http\JsonResponse */ public function authenticate(AuthJwtRequest $request) { $credentials = $request->only(['email', 'password']); // Attempt to verify the credentials and create a token for the user if (!($token = JWTAuth::attempt($credentials))) { $response = $this->response->error(ResponseCode::UNAUTHORIZED, trans('texts.invalid_credentials')); } else { $data = ['token' => $token, 'user' => JWTAuth::authenticate($token)]; $response = $this->response->ok($data); } return $response; }
public function testNewCompany() { $response = $this->json('POST', '/companies/auth/register', ['name' => 'test', 'email' => '*****@*****.**', 'password_confirmation' => 'testtest', 'password' => 'testtest']); $response->seeStatusCode(200); $this->seeInDatabase("companies", ['name' => 'test', 'email' => '*****@*****.**']); $token = json_decode($response->response->content(), true); $token = $token['token']; /** * @var $company \plunner\Company */ $company = JWTAuth::authenticate($token); $this->assertEquals('*****@*****.**', $company->email); }
/** * Bootstrap the application services. * * @param Request $request */ public function boot(Request $request) { view()->composer('index', function ($view) use($request) { $jwt_token = $request->cookie('jwt'); if ($jwt_token) { try { $user = JWTAuth::authenticate($jwt_token); $view->with('user', $user); } catch (\Exception $e) { } } }); }
/** * Create Session for FE * @param Request $request */ public function createSession(Request $request) { $session = md5(time()); $create = $request->all(); $create['session'] = $session; $token = JWTAuth::getToken(); if ($token) { try { $user = JWTAuth::authenticate($token); if ($user != false) { $create['user_id'] = $user->id; } } catch (\Exception $e) { } } $data = $this->dataRepository->insert($create); if ($data instanceof MessageBag) { return response()->json($data)->setStatusCode(412, 'Invalid session create'); } return response()->json(['session' => $session])->setStatusCode(200, 'Session created'); }