-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
115 lines (86 loc) · 3.71 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Passport\HasApiTokens;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Http\Request;
use Laravel\Passport\Client as OClient;
use Laravel\Passport\TokenRepository;
use Laravel\Passport\RefreshTokenRepository;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use HasApiTokens, Authenticatable, Authorizable, HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'email', 'password', 'token', 'password_reset_link_at', 'email_verified_at'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'token', 'password_reset_link_at', 'email_verified_at', 'created_at', 'updated_at'
];
public function sendVerificationEmail(){
$token = base64_encode($this->token);
$email_link = route('user.emailVerificationLink', ['token' => $token]);
//send mail
return $email_link;
}
public function sendForgetPasswordEmail(){
$token = base64_encode($this->token);
$reset_password_link = url("reset-password/".$token);
//send mail
return $reset_password_link;
}
public function getTokenAndRefreshToken($email, $password) {
$oClient = OClient::where('password_client', 1)->first();
$form_params = [
'grant_type' => 'password',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'username' => $email,
'password' => $password,
'scope' => '*',
];
$request = Request::create(env('APP_URL').'/api/v1/oauth/token', 'POST', $form_params);
$response = app()->handle($request);
$content = $response->getContent();
$statusCode = $response->getStatusCode();
//return response($content, $statusCode)->header('Content-Type', 'application/json');
return [json_decode($content, true), $statusCode];
}
public static function getAccessTokenFromRefreshToken($refresh_token) {
$oClient = OClient::where('password_client', 1)->first();
$form_params = [
'grant_type' => 'refresh_token',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'refresh_token' => $refresh_token,
'scope' => '*',
];
$request = Request::create(env('APP_URL').'/api/v1/oauth/token', 'POST', $form_params);
$response = app()->handle($request);
$content = $response->getContent();
$statusCode = $response->getStatusCode();
//return response($content, $statusCode)->header('Content-Type', 'application/json');
return [json_decode($content, true), $statusCode];
}
public function logOut($tokenId, $tokenRepository=null, $refreshTokenRepository=null){
!$tokenRepository ? $tokenRepository = app(TokenRepository::class) : "";
!$refreshTokenRepository ? $refreshTokenRepository = app(RefreshTokenRepository::class) : "";
// Revoke an access token
$tokenRepository->revokeAccessToken($tokenId);
// Revoke all of the token's refresh tokens
$refreshTokenRepository->revokeRefreshTokensByAccessTokenId($tokenId);
}
}