本文整理汇总了PHP中Tymon\JWTAuth\Facades\JWTAuth::attempt方法的典型用法代码示例。如果您正苦于以下问题:PHP JWTAuth::attempt方法的具体用法?PHP JWTAuth::attempt怎么用?PHP JWTAuth::attempt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tymon\JWTAuth\Facades\JWTAuth
的用法示例。
在下文中一共展示了JWTAuth::attempt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: postLogin
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
$usernames = $this->loginUsername();
if (!is_array($usernames)) {
$usernames = [$usernames];
}
$usernamesR = [];
foreach ($usernames as $username) {
$usernamesR[$username] = 'required';
}
$this->validate($request, array_merge($usernamesR, ['password' => 'required']));
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if ($token = JWTAuth::attempt($credentials, $this->customClaims())) {
return $this->handleUserWasAuthenticated($request, $throttles, $token);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return new JsonResponse([implode('.', $usernames) => [$this->getFailedLoginMessage()]], 422);
}
示例2: login
public function login(Request $request)
{
$data = $request->only('username', 'password');
$gcm_token = $request->get('gcm_token');
$throttler = Throttle::get($request, 5, 1);
if (!$throttler->check()) {
$response = Response(['error' => 'too many wrong requests']);
$response->setStatusCode('420', "Enhance Your Calm");
return $response;
}
try {
if (!($token = JWTAuth::attempt($data))) {
// Invalid authentication, hit the throttler
$throttler->hit();
return response()->json(['error' => true, 'message' => 'invalid_credentials'], 401);
}
// We have a google token, so let's set it
if (null != $gcm_token) {
$user = JWTAuth::toUser($token);
$user->setGCMToken($gcm_token);
$user->save();
}
} catch (JWTException $e) {
$throttler->hit();
return response()->json(['error' => true, 'message' => 'couldnt_create_token'], 401);
}
return response()->json(compact('token'));
}
示例3: authenticate
public function authenticate(Request $request)
{
// \Cache::flush();
// grab credentials from the request
$credentials = $request->only('name', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (!($token = JWTAuth::attempt($credentials))) {
return response()->json(['message' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['message' => 'could_not_create_token'], 500);
}
//cek double login
$token = compact('token');
Input::merge(['token' => $token['token']]);
$this->model = $this->model->where('name', $credentials['name'])->first();
if ($request->get('name') != 'admin') {
//user is login
$tempStorage = app('\\App\\Http\\Controllers\\TEMPStorage\\UserTempStorage');
if (!empty($tempStorage->get('id_company', $this->model->id))) {
return response()->json(['message' => trans('user_already_login')], 500);
}
}
// all good so return the token
$this->model->update(['login' => 1]);
$data = json_decode($this->item($this->model, $this->transformer)->morph()->getContent(), true) + $token;
return $this->response->array($data)->withHeader('Content-Type', 'application/json');
// return response()->json(compact('token'));
}
示例4: authenticate
/**
* Autheticate user using email and password
* @param Request $request
* @return array
*/
public function authenticate(Request $request)
{
# validate the request
$validator = Validator::make($request->all(), ['email' => 'required|email', 'password' => 'required|min:6']);
if ($validator->fails()) {
return jsend()->error()->message('Invalid Input')->errors($validator->errors()->all())->get();
}
# grab credentials from the request
$credentials = $request->only('email', 'password');
# verify that the email does not belong to a provider
$user = User::where(['email' => $credentials['email']])->first();
if (!empty($user->provider)) {
return jsend()->error()->message('E-Mail has been registered using ' . $user->provider)->errors($validator->errors()->all())->get();
}
try {
# attempt to verify the credentials and create a token for the user
if (!($token = JWTAuth::attempt($credentials))) {
return jsend()->error()->message('Invalid Credentials. Login Failed.')->get();
}
} catch (JWTException $e) {
# Something went wrong whilst attempting to encode the token
return jsend()->error()->message('Could not create token.')->errors([$e->getMessage()])->get();
}
$user = Auth::user();
if ($user->verified) {
# All good so return the token
return jsend()->success()->message('User logged in successfully')->data(['user' => $user, 'api-token' => $token])->get();
} else {
# User not verified yet
return jsend()->error()->message('Account Activation Pending. Please check your E-Mail.')->get();
}
}
示例5: signin
public function signin()
{
$credentials = Input::only('email', 'password');
if (!($token = JWTAuth::attempt($credentials))) {
return Response::json(false, HttpResponse::HTTP_UNAUTHORIZED);
}
return Response::json(compact('token'));
}
示例6: postLogin
public function postLogin(Request $request)
{
$credentials = $request->only('email', 'password');
$token = JWTAuth::attempt($credentials);
if (!$token) {
return response()->json('Incorrect username or password combination.', Response::HTTP_UNAUTHORIZED);
}
return response()->json(compact('token'));
}
示例7: signin
/**
* @param SigninRequest $request
* @return mixed
*/
public function signin(SigninRequest $request)
{
$credentials = $request->only('email', 'password');
if (!($token = JWTAuth::attempt($credentials))) {
return $this->response->withError('Combinaison email / mot de passe érronée', 401);
}
$user = Auth::user();
// if no errors are encountered we can return a JWT
return $this->response->array(['token' => $token, 'user' => $user->toArray()]);
}
示例8: login
/**
* Token-based Authentication login.
*
* @param LoginUserRequest $request
*
* @return Response
*
* @api {post} /v1/auth/login User Login
* @apiVersion 1.0.0
* @apiName AuthLogin
* @apiGroup Authentication
*
* @apiParam {String} email Email of the User.
* @apiParam {String} Password Password of the User.
*
* @apiSuccess {String} token Authentication token.
*
* @apiUse ApiLimitError
*/
public function login(LoginUserRequest $request)
{
$credentials = $request->only(['email', 'password']);
try {
if (!($token = JWTAuth::attempt($credentials))) {
return $this->response->errorUnauthorized();
}
} catch (JWTException $e) {
return $this->response->error('could_not_create_token', 500);
}
return response()->json(compact('token'));
}
示例9: login
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
try {
if (!($token = JWTAuth::attempt($credentials))) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
示例10: testLogout
public function testLogout()
{
$credentials = ['email' => 'admin@example.com', 'password' => '123456'];
$token = JWTAuth::attempt($credentials);
$this->assertEquals('admin@example.com', Auth::user()->email);
$res = $this->call('POST', '/auth/logout', [], [], [], ['HTTP_Authorization' => "Bearer {$token}"]);
$this->assertEquals(204, $res->getStatusCode());
$this->assertNull(Auth::user());
// check re-logout
$res = $this->call('POST', '/auth/logout', [], [], [], ['HTTP_Authorization' => "Bearer {$token}"]);
$this->assertEquals(401, $res->getStatusCode());
}
示例11: authenticate
/**
* 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;
}
示例12: signin
protected function signin(Request $request)
{
$credentials = $request->only('email', 'password');
try {
if (!($token = JWTAuth::attempt($credentials))) {
return response()->json(['error' => 'Could not log you in.'], 401);
}
} catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['error' => 'Could not create token.'], 500);
}
return response()->json(compact('token'));
}
示例13: authenticate
public function authenticate(Request $request)
{
$user = User::where('email', $request->email)->first();
if (!$user) {
throw new BadRequestHttpException('Invalid credentials');
}
$customClaims = ['name' => $user->name, 'email' => $user->email, 'role' => $user->role, 'gravatar' => $user->gravatar];
$token = JWTAuth::attempt($request->only('email', 'password'), $customClaims);
if (!$token) {
throw new BadRequestHttpException('Invalid credentials');
}
return api_response(200, compact('token'));
}
示例14: authenticate
function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
if (!($token = JWTAuth::attempt($credentials))) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
示例15: postLogin
public function postLogin(Request $request)
{
$credentials = $request->only('email', 'password');
try {
Auth::attempt($credentials);
// attempt to verify the credentials and create a token for the user
if (!($token = JWTAuth::attempt($credentials))) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
return Response::json(['token' => $token, 'data' => $this->transform(Auth::user())], 200);
}