@@ -67,7 +67,7 @@ public function isConnected()
67
67
* Make a connection or return a token where it's valid.
68
68
* @return mixed
69
69
*/
70
- public function connect ()
70
+ public function connect ($ redirect = true )
71
71
{
72
72
try {
73
73
$ params = [
@@ -80,8 +80,13 @@ public function connect()
80
80
$ token = $ this ->dopost (config ('msgraph.tenantUrlAccessToken ' ), $ params );
81
81
82
82
// Store token
83
- return $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
83
+ $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
84
84
85
+ if ($ redirect ) {
86
+ return redirect (config ('msgraph.msgraphLandingUri ' ));
87
+ }
88
+
89
+ return $ token ->access_token ;
85
90
86
91
} catch (Exception $ e ) {
87
92
throw new Exception ($ e ->getMessage ());
@@ -94,28 +99,36 @@ public function connect()
94
99
* @param $returnNullNoAccessToken null when set to true return null
95
100
* @return return string access token
96
101
*/
97
- public function getAccessToken ($ returnNullNoAccessToken = null )
102
+ public function getAccessToken ($ returnNullNoAccessToken = false , $ redirect = false )
98
103
{
99
104
// Admin token will be stored without user_id
100
105
$ token = MsGraphToken::where ('user_id ' , null )->first ();
101
106
102
107
// Check if tokens exist otherwise run the oauth request
103
108
if (! isset ($ token ->access_token )) {
104
109
// Don't request new token, simply return null when no token found with this option
105
- if ($ returnNullNoAccessToken == true ) {
110
+ if ($ returnNullNoAccessToken ) {
106
111
return null ;
107
112
}
108
- // Run the oath request and return new token
109
- return $ this ->connect ()->access_token ;
113
+
114
+ if ($ redirect ) {
115
+ return $ this ->connect ($ redirect );
116
+ }
117
+
118
+ return $ this ->connect ($ redirect )->access_token ;
110
119
}
111
120
112
- // Check if token is expired
113
- // Get current time + 5 minutes (to allow for time differences)
114
- $ now = time () + 300 ;
115
- if ($ token ->expires <= $ now ) {
116
- // Token is expired (or very close to it) so let's refresh
117
- return $ this ->connect ()->access_token ;
121
+ $ now = now ()->addMinutes (5 );
122
+
123
+ if ($ token ->expires < $ now ) {
124
+ if ($ redirect ) {
125
+ return $ this ->connect ($ redirect );
126
+ }
127
+
128
+ return $ this ->connect ($ redirect );
129
+
118
130
} else {
131
+
119
132
// Token is still valid, just return it
120
133
return $ token ->access_token ;
121
134
}
0 commit comments