NoroffFEU / ai-project

The Unlicense
1 stars 0 forks source link

login logic #5

Open Teitrheim opened 4 months ago

Teitrheim commented 4 months ago

how to create google login.

Ulvounth commented 4 months ago

How to Sign In with Google Google's How to get started documentation

1. Google Cloud Platform Setup Create a Project: Go to the Google Cloud Console, create a new project or select an existing one. Enable APIs: Navigate to "APIs & Services > Dashboard", search for "Google Identity Services", and enable it. Configure OAuth Consent Screen: Under "APIs & Services", go to "OAuth consent screen" and provide the necessary details about your application. Create OAuth 2.0 Client IDs: In "Credentials", click "+ CREATE CREDENTIALS" and select "OAuth 2.0 Client IDs". Fill in the application type, authorized JavaScript origins, and redirect URIs.

2. Frontend Implementation Load Google Identity Services Library: Include in your HTML. Add a Google Sign-In Button: Use the div id="g_id_onload" data-client_id="YOUR_CLIENT_ID" data-callback="handleCredentialResponse">div" to add a sign-in button. Replace "YOUR_CLIENT_ID" with your actual client ID. Handle Sign-In Response: Implement a handleCredentialResponse function in your JavaScript to handle the sign-in response, which includes the user's ID token. Parse User Information: Optionally, parse the ID token (JWT) to extract user information like name, email, and profile picture.

3. Backend Verification (Optional but Recommended) Send ID Token to Your Server: After successful sign-in, send the ID token to your server using an HTTP request. Verify ID Token: Use Google's library (available in multiple languages) on the server to verify the token's integrity, ensuring it's valid and intended for your application. Create/Update User Session: Based on the verified token, create or update the user session in your application.

4. Usage Access Protected Resources: Use the access token obtained through this process to call Google APIs or access protected resources in your application on behalf of the signed-in user. Sign Out: Implement a sign-out mechanism by using google.accounts.id.disableAutoSelect() and clearing the application's session.