Home > Web Front-end > JS Tutorial > A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

Susan Sarandon
Release: 2025-01-29 08:35:08
Original
806 people have browsed it

This guide demonstrates a fundamental JavaScript and Express server implementation of Google OAuth2 user authentication. While libraries simplify the process, this hands-on approach clarifies core concepts. We'll use Bun, a fast JavaScript runtime.


Google OAuth2 Setup

Before coding, configure your Google Cloud project:

Step 1: Create a Google Cloud Project

  1. Access the Google Cloud Console.
  2. Create a new project (or use an existing one). A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

Step 2: Configure OAuth Consent Screen

  1. Navigate to API & Services > OAuth Consent Screen.
  2. Provide app name, support email, and other required details. A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun
  3. (Optional) Customize branding for a more informative consent screen. A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

Step 3: Define Scopes

  1. Under Scopes, add:
    • email: Access user's email.
    • openid: OpenID Connect for identity verification.
    • profile: Access basic profile data (name, picture). A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

Step 4: Generate OAuth Client ID

(Allow a few minutes for processing.)

  1. Go to Credentials > Create Credentials > OAuth Client ID.
  2. Choose Web Application as the application type.
  3. Set Authorized JavaScript Origins to http://localhost:3000.
  4. Specify a Redirect URI: http://localhost:3000. Remember to update these URIs for production deployment. A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun Google redirects users to the Redirect URI after authentication, including an authorization code and state. For simplicity, we use the same page. A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun

Step 5: Add Test Users

For testing, add the email addresses of your test users. Publish your app after thorough testing. A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun


Google OAuth2 Code Implementation

Project Structure

<code>project/
├── public/
│   └── index.html
├── src/
│   └── index.js
├── .env
└── bun.lock</code>
Copy after login

We use Bun version 1.2.

index.html

This HTML provides a simple interface for Google Sign-In and OAuth callback handling. (Code omitted for brevity, but included in the original prompt)

index.js (Server-side logic)

(Code omitted for brevity, but included in the original prompt)

Running the Project

  1. Install Bun (instructions available on the Bun website).
  2. Create a .env file with your GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, and GOOGLE_REDIRECT_URI.
  3. Run the server: bun run index.js
  4. Test the flow: Access http://localhost:3000, sign in with Google, and check the console for tokens.

Conclusion

This manual implementation provides a strong understanding of Google OAuth2. While libraries like Passport or NextAuth offer convenience, this approach illuminates the underlying authentication mechanisms.

The above is the detailed content of A Step-by-Step Guide to Google OAuthuthentication with JavaScript and Bun. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template