本文整理匯總了PHP中Token::user方法的典型用法代碼示例。如果您正苦於以下問題:PHP Token::user方法的具體用法?PHP Token::user怎麽用?PHP Token::user使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Token
的用法示例。
在下文中一共展示了Token::user方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: login
public function login($id)
{
$user = User::where('username', '=', $id)->firstOrFail();
if (Hash::check(Input::get('password'), $user->password)) {
if (!is_null($user->token)) {
$user->token->delete();
}
$token = new Token();
$token->token = Hash::make($user->username);
$token->expiration_date = Carbon::now()->addDay();
$token->user()->associate($user);
$token->save();
$user->token()->save($token);
$user->save();
return Response::json(array('token' => $token->token), 200);
} else {
return Response::json(array('message' => 'invalid password!'), 401);
}
}
示例2: User
// Check if username exists
if (User::where('nickname', 'like', $req_body->nickname)->count()) {
$app->halt(400, 'nickname_exists');
}
try {
$user = new User();
$user->nickname = $req_body->nickname;
$user->gender = $req_body->gender;
$user->password = sha1($req_body->password1);
$user->educationLevel()->associate(EducationLevel::find((int) $req_body->schoolAdvice));
$user->school()->associate(School::find((int) $req_body->school));
$user->save();
// Create token
$token = new Token();
$token->generateToken();
$token->user()->associate($user);
$token->save();
} catch (Exception $e) {
$app->halt(500, 'something_went_wrong');
}
echo $token->toJson();
});
$app->get('/user', function () use($app) {
$app->response()->header('Content-Type', 'application/json');
$token_key = $app->request->headers->get('Authorization');
try {
$user = User::with('talents', 'token')->whereHas('token', function ($q) use($token_key) {
$q->where('key', '=', $token_key);
})->firstOrFail();
} catch (ModelNotFoundException $e) {
$app->halt(401, 'Gebruiker niet gevonden');
示例3: generateToken
/**
* Generates a login token for the given username
* @param string
* @return void
*/
private function generateToken($username)
{
$user = User::where('username', '=', $username)->first();
$token = new Token();
$token->token = Hash::make($user->username);
$token->expiration_date = Carbon::now()->addDay();
$token->user()->associate($user);
$token->save();
$user->token()->save($token);
$user->save();
}