Skip to content

Commit 83abf3b

Browse files
committed
Update oauth.md
1 parent c5bca9e commit 83abf3b

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

docs/oauth.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# OAuth 2.0 Authentication
22

3-
OAuth 2.0 authentication allows users to grant your application access to their personal Google Sheets. This method is ideal for user-facing applications where each user needs to access their own spreadsheets.
3+
OAuth 2.0 authentication allows users to grant your application access to their personal Google Sheets. This method is
4+
ideal for user-facing applications where each user needs to access their own spreadsheets.
45

56
## When to Use OAuth Authentication
67

@@ -21,21 +22,21 @@ OAuth 2.0 authentication allows users to grant your application access to their
2122
2. Select your project or create a new one
2223
3. Navigate to **APIs & Services** > **Library**
2324
4. Enable the following APIs:
24-
- **Google Sheets API**
25-
- **Google Drive API**
25+
- **Google Sheets API**
26+
- **Google Drive API**
2627

2728
## Step 2: Create OAuth 2.0 Credentials
2829

2930
1. Go to **APIs & Services** > **Credentials**
3031
2. Click **Create Credentials** > **OAuth client ID**
3132
3. If prompted, configure the OAuth consent screen:
32-
- Choose **External** for public applications
33-
- Fill in required fields (app name, user support email, developer contact)
34-
- Add scopes: `https://www.googleapis.com/auth/spreadsheets` and `https://www.googleapis.com/auth/drive`
33+
- Choose **External** for public applications
34+
- Fill in required fields (app name, user support email, developer contact)
35+
- Add scopes: `https://www.googleapis.com/auth/spreadsheets` and `https://www.googleapis.com/auth/drive`
3536
4. For application type, choose **Web application**
3637
5. Add authorized redirect URIs:
37-
- For development: `http://localhost:8000/auth/google/callback`
38-
- For production: `https://yourdomain.com/auth/google/callback`
38+
- For development: `http://localhost:8000/auth/google/callback`
39+
- For production: `https://yourdomain.com/auth/google/callback`
3940
6. Click **Create**
4041
7. Copy the **Client ID** and **Client Secret**
4142

@@ -98,13 +99,10 @@ class AuthController extends Controller
9899
public function redirectToGoogle()
99100
{
100101
return Socialite::driver('google')
101-
->scopes([
102-
'https://www.googleapis.com/auth/spreadsheets',
103-
'https://www.googleapis.com/auth/drive'
104-
])
102+
->scopes(config('google.scopes'))
105103
->with([
106-
'access_type' => 'offline',
107-
'prompt' => 'consent select_account'
104+
'access_type' => config('google.access_type'),
105+
'prompt' => config('google.prompt'),
108106
])
109107
->redirect();
110108
}
@@ -139,7 +137,7 @@ class AuthController extends Controller
139137
auth()->logout();
140138
$request->session()->invalidate();
141139
$request->session()->regenerateToken();
142-
140+
143141
return redirect('/');
144142
}
145143
}
@@ -221,7 +219,7 @@ public function getSheetData(Request $request)
221219

222220
try {
223221
$token = $user->getGoogleTokenArray();
224-
222+
225223
$values = Sheets::setAccessToken($token)
226224
->spreadsheet('user-spreadsheet-id')
227225
->sheet('Sheet1')
@@ -233,7 +231,7 @@ public function getSheetData(Request $request)
233231
if (str_contains($e->getMessage(), 'invalid_grant') || str_contains($e->getMessage(), 'unauthorized')) {
234232
return redirect()->route('google.redirect');
235233
}
236-
234+
237235
throw $e;
238236
}
239237
}
@@ -284,12 +282,12 @@ class RequireGoogleAuth
284282
public function handle(Request $request, Closure $next)
285283
{
286284
$user = $request->user();
287-
285+
288286
if (!$user || !$user->hasValidGoogleToken()) {
289287
if ($request->expectsJson()) {
290288
return response()->json(['error' => 'Google authentication required'], 401);
291289
}
292-
290+
293291
return redirect()->route('google.redirect');
294292
}
295293

@@ -301,16 +299,19 @@ class RequireGoogleAuth
301299
## Security Considerations
302300

303301
### 1. Token Storage
302+
304303
- Store tokens securely in the database
305304
- Use Laravel's built-in encryption for sensitive fields
306305
- Never expose tokens in client-side code
307306

308307
### 2. Scope Management
308+
309309
- Only request necessary scopes
310310
- Use least-privilege principle
311311
- Clearly explain to users what access you need
312312

313313
### 3. Error Handling
314+
314315
- Handle expired tokens gracefully
315316
- Provide clear re-authentication flows
316317
- Log authentication errors for monitoring
@@ -320,16 +321,19 @@ class RequireGoogleAuth
320321
### Common OAuth Errors
321322

322323
**"redirect_uri_mismatch"**
324+
323325
- Ensure redirect URI in Google Console matches exactly with your application
324326
- Check for http vs https mismatches
325327
- Verify trailing slashes match
326328

327329
**"invalid_grant" or "unauthorized"**
330+
328331
- Token has expired and refresh failed
329332
- Redirect user to re-authenticate
330333
- Check if refresh token is available
331334

332335
**"access_denied"**
336+
333337
- User denied permission
334338
- Handle gracefully with appropriate messaging
335339
- Provide option to retry authentication
@@ -356,7 +360,3 @@ Route::get('/test-oauth', function (Request $request) {
356360
}
357361
})->middleware('auth');
358362
```
359-
360-
## Example Implementation
361-
362-
For a complete working example, see the [Laravel Google Sheets Demo Project](https://github.com/kawax/google-sheets-project/blob/6.x/app/Http/Controllers/LoginController.php).

0 commit comments

Comments
 (0)