Skip to content

diegoperea20/ASP.NET-10-WEB-API-Google-Auth-Backend-Tasks

Repository files navigation

ASP.NET 10 WEB API Google Auth Backend Tasks

Backend built with ASP.net 10 with sqlite db, implementing robust authentication via JWT and Google OAuth. It is designed to be secure, fast crud tasks, and easy to integrate with modern frontends like Next.js.

Same project in other backend Python:

FastApi

Flask

Django

Frontend:

Nextjs

πŸ›£οΈ Main Endpoints

Traditional Authentication

  • POST /api/register: Register new users.
  • POST /api/login: Log in with username/password.

Google OAuth

  • GET /api/login/google: Starts the Google flow.
  • GET /authorize/google: Callback that processes Google's response.
  • POST /api/auth/google/exchange: Exchanges the temporary code for a JWT token.

Protected (Require Bearer Token)

  • GET /api/user/profile: Returns the current user's profile.
  • GET /api/protected: Test endpoint to verify authorization.
  • GET /api/stats: Basic system statistics.

πŸ›‘οΈ Implemented Security

  • Hashing: Direct use of bcrypt to avoid compatibility issues.

  • JWT Authentication: Traditional login and registration with username/password
  • OAuth 2.0 Authentication: Login with Google

Tech Stack

  • .NET 10 - Web API framework
  • Sqlite - Database

Getting Started

Prerequisites

  • .NET 10 SDK

  • PowerShell (for Windows)

Installation

  1. Clone the repository:
git clone <repository-url>
cd <repository-url>
  1. Edit the run-with-env.ps1 file with your own credentials.

Run with environment variables

powershell -ExecutionPolicy Bypass -File .\run-with-env.ps1

Create migration

 dotnet ef migrations add InitialCreate

Update database

 dotnet ef database update
  1. Configure environment variables

Configure Google OAuth 2.0

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google+ API (or the Google People API for modern apps)
  4. Go to "Credentials" > "Create credentials" > "OAuth 2.0 Client ID"
  5. Configure:
    • Application type: Web application
    • Authorized redirect URIs: http://localhost:5205/authorize/google and http://127.0.0.1:5205/authorize/google
  6. Copy the Client ID and Client Secret to your appsettings.json or run-with-env.ps1 file

Create Google Console Project and enable the API:

The API will be available at http://localhost:5205

Create the ASP.NET Web API project:

dotnet new webapi -n TaskGo --use-controllers
 dotnet run
 dotnet build

Search packages or libraries

search packages or libraries in nuget

πŸ“š API Endpoints

JWT Authentication

POST /api/register

Registers a new user.

Request Body:

{
  "username": "user@example.com",
  "password": "password123"
}

Response (201):

{
  "message": "User registered successfully",
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "user": {
    "id": 1,
    "username": "user@example.com"
  }
}

POST /api/login

Logs in with username and password.

Request Body:

{
  "username": "user@example.com",
  "password": "password123"
}

Response (200):

{
  "message": "Login successful",
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "user": {
    "id": 1,
    "username": "user@example.com"
  }
}

Google OAuth Authentication

GET /api/login/google

Starts the Google authentication process. Redirects the user to Google for authorization.

GET /api/authorize/google

Google OAuth callback. Redirects the frontend with a temporary authorization code.

Protected Routes

GET /api/protected

Protected route that requires a valid JWT token.

Headers:

Authorization: Bearer <token>

Response (200):

{
  "message": "Access authorized",
  "user": {
    "id": 1,
    "username": "user@example.com"
  }
}

GET /api/user/profile

Retrieves the profile of the authenticated user.

Headers:

Authorization: Bearer <token>

Response (200):

{
  "user": {
    "id": 1,
    "username": "user@example.com"
  }
}

πŸ”’ JWT Token Usage

After authenticating (login or Google), you will receive an access_token. To access protected routes, include this token in the Authorization header:

Authorization: Bearer <access_token>

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Diego Ivan Perea Montealegre


Created by Diego Ivan Perea Montealegre

About

Backend built with ASP.net 10, implementing robust authentication via JWT and Google OAuth. It is designed to be secure, fast crud tasks, and easy to integrate with modern frontends like Next.js.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors