Skip to content

Custom Scheme

File Information

  • Path: schemes/customScheme.js
  • Purpose: Main authentication scheme for regular workspace users
  • Strategy Name: custom

Overview

The Custom Scheme provides authentication functionality for regular workspace users. It handles email/password login, token management, user data fetching, workspace context, and session management.

Key Features

  1. Login Functionality

    • Email/password authentication
    • Token generation and storage
    • User data retrieval
    • Workspace context setup
  2. Token Management

    • Access token storage
    • Refresh token handling
    • Token expiration management
    • Automatic token injection
  3. User Management

    • User data fetching
    • User state updates
    • Workspace information
    • Permission data
  4. Session Management

    • Session creation
    • Session validation
    • Session refresh
    • Cross-tab sync

Scheme Methods

mounted()

Called when auth module is mounted:

  • Initializes scheme
  • Checks existing session
  • Validates stored tokens
  • Restores user session if valid

login(endpoint, data)

Handles user login:

  • Sends login request
  • Receives authentication tokens
  • Stores tokens
  • Fetches user data
  • Sets workspace context
  • Returns user data

Parameters:

  • endpoint (Object): Login endpoint configuration
  • data (Object): Login credentials
    • email (string): User email
    • password (string): User password

Returns: Promise resolving to user object

logout(endpoint)

Handles user logout:

  • Sends logout request
  • Clears tokens
  • Clears user data
  • Clears workspace context
  • Redirects to login

Parameters:

  • endpoint (Object): Logout endpoint configuration

Returns: Promise

fetchUser(endpoint)

Fetches current user data:

  • Sends user request with token
  • Receives user data
  • Updates user state
  • Updates workspace context
  • Returns user object

Parameters:

  • endpoint (Object): User endpoint configuration

Returns: Promise resolving to user object

reset()

Resets authentication state:

  • Clears tokens
  • Clears user data
  • Clears workspace context
  • Resets auth state

Token Storage

Tokens are stored in:

  • Cookies: HTTP-only cookies for security
  • localStorage: Additional token data
  • Vuex Store: User state

API Endpoints

Login

  • Endpoint: POST /login
  • Request Body:
    json
    {
      "email": "[email protected]",
      "password": "password"
    }
  • Response:
    json
    {
      "token": "jwt_access_token",
      "refresh_token": "refresh_token",
      "user": {
        "id": 1,
        "email": "[email protected]",
        "name": "User Name",
        "workspaces": []
      }
    }

Logout

  • Endpoint: POST /logout
  • Request: Includes authentication token
  • Response: Success confirmation

Get User

  • Endpoint: GET /user
  • Request: Includes authentication token
  • Response: User object with workspaces

Workspace Context

The scheme manages workspace context:

  • Current workspace selection
  • Workspace switching
  • Workspace permissions
  • Workspace modules

Usage Examples

Login

javascript
// In component
try {
  const response = await this.$auth.loginWith('custom', {
    data: {
      email: '[email protected]',
      password: 'password'
    }
  })
  // User logged in
  this.$router.push('/dashboard')
} catch (error) {
  // Handle login error
  this.$snackbar.error('Login failed')
}

Check Authentication

javascript
// In component
if (this.$auth.loggedIn) {
  const user = this.$auth.user
  const workspace = this.$auth.user.currentWorkspace
}

Logout

javascript
// In component
await this.$auth.logout()
this.$router.push('/login')

Integration Points

  • Login Page: Uses login method
  • Middleware: Checks loggedIn status
  • API Plugin: Injects tokens
  • Store: Manages user state

Notes for Development

  • Scheme extends auth-next base
  • Tokens stored securely
  • User data cached
  • Workspace context managed
  • Error handling required