01. G6 MW Authentication

Authentication

Gen6 dApps uses a two-tier authentication system combining blockchain wallet signatures with JWT tokens for secure API access (all provided through G6 MW instance(s)).

Overview

Authentication requires:

  1. Polkadot Wallet - User must have a connected wallet (via extension or Google auth)

  2. JWT Token - User must sign a challenge to prove wallet ownership

The JWT token is stored in an HttpOnly cookie and included automatically in all API requests.

Authentication Flow

Step 1: Get Challenge

Request a signing challenge from the backend.

Endpoint: POST /auth/challenge

Request:

const response = await fetch(`${apiUrl}/auth/challenge`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  credentials: 'include',
  body: JSON.stringify({
    public_address: g6_address // SS58-355 encoded address
  })
})

const data = await response.json()

Request Body:

Response:

Example:


Step 2: Sign Challenge

Sign the challenge using the Polkadot wallet.

Implementation:


Step 3: Verify Signature

Send the signed challenge to verify and obtain a JWT token.

Endpoint: POST /auth/verify

Request:

Request Body:

Response:

  • Sets HttpOnly cookie with JWT token

  • Returns success status

Cookie Details:

  • Name: Session cookie (managed by backend)

  • HttpOnly: true (not accessible via JavaScript)

  • Secure: true (HTTPS only in production)

  • SameSite: Lax


Step 4: Verify Authentication Status

Check if the current user is authenticated.

Endpoint: GET /auth/jwt_ping

Implementation:

Response:

The response structure depends on the backend implementation. Check the actual response format returned by the backend.

Usage in React:

Query Configuration:


Faucet

Request test tokens from the faucet.

Endpoint: POST /faucet/request

Requirements:

  • Authenticated user (JWT token in HttpOnly cookie)

Request:

Response:

Note: Faucet is automatically called after successful authentication for Google auth users.


Route Guards

Protect routes with authentication checks.

Pattern:


Complete Authentication Hook

The useAuthorize hook combines all authentication steps.

Location: src/packages/auth/hooks/useAuthorize.tsx

Usage:

Implementation Details:


Axios Configuration

All authenticated API calls use the centralized axios instance.

Location: src/packages/auth/api/axiosInstance.ts

Configuration:


Security Best Practices

JWT Token Management

  1. HttpOnly cookies - Tokens not accessible via JavaScript (XSS protection)

  2. Secure flag - HTTPS only in production


Common Issues

"Authentication failed"

  • Cause: Wallet not connected or signature invalid

  • Solution: Ensure wallet extension is installed and unlocked

"CORS error"

  • Cause: Origin not whitelisted on backend

  • Solution: Verify origin parameter matches backend whitelist


Address Encoding

Gen6 uses SS58 address format with prefix 355.

Conversion:

Example:

Always use the Gen6-encoded address for API requests.


Next Steps

  • Identity Management - Create and manage user profiles

  • Real-Seal - Sign documents and texts

  • Ncrypt - Send encrypted messages

Last updated

Was this helpful?