@@ -67,7 +67,7 @@ public function isConnected()
6767 * Make a connection or return a token where it's valid.
6868 * @return mixed
6969 */
70- public function connect ()
70+ public function connect ($ redirect = true )
7171 {
7272 try {
7373 $ params = [
@@ -80,8 +80,13 @@ public function connect()
8080 $ token = $ this ->dopost (config ('msgraph.tenantUrlAccessToken ' ), $ params );
8181
8282 // Store token
83- return $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
83+ $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
8484
85+ if ($ redirect ) {
86+ return redirect (config ('msgraph.msgraphLandingUri ' ));
87+ }
88+
89+ return $ token ->access_token ;
8590
8691 } catch (Exception $ e ) {
8792 throw new Exception ($ e ->getMessage ());
@@ -94,28 +99,36 @@ public function connect()
9499 * @param $returnNullNoAccessToken null when set to true return null
95100 * @return return string access token
96101 */
97- public function getAccessToken ($ returnNullNoAccessToken = null )
102+ public function getAccessToken ($ returnNullNoAccessToken = false , $ redirect = false )
98103 {
99104 // Admin token will be stored without user_id
100105 $ token = MsGraphToken::where ('user_id ' , null )->first ();
101106
102107 // Check if tokens exist otherwise run the oauth request
103108 if (! isset ($ token ->access_token )) {
104109 // Don't request new token, simply return null when no token found with this option
105- if ($ returnNullNoAccessToken == true ) {
110+ if ($ returnNullNoAccessToken ) {
106111 return null ;
107112 }
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 ;
110119 }
111120
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+
118130 } else {
131+
119132 // Token is still valid, just return it
120133 return $ token ->access_token ;
121134 }
0 commit comments