Back to SDK Reference

Authentication Methods

Complete reference for all authentication functions

signIn()

Authenticate users with email and password

signIn(email, password): Promise<Result>

signOut()

Log out users and clear authentication state

signOut(): Promise<Result>

signUp()

Register new users with email, password and username

signUp(email, password, username, emailConfig?): Promise<Result>

forgotPassword()

Send password reset email to user

forgotPassword(email): Promise<Result>

signIn()

The signIn method authenticates users with email and password. The server automatically detects your application URL from request headers, so you don't need to provide it manually.

Basic Usage

import { signIn } from 'easyauth';

// Sign in with email and password
const result = await signIn('user@example.com', 'password123');

if (result.success) {
  console.log('Login successful!');
  console.log('User:', result.data.user);
  // Tokens are automatically stored in secure cookies
} else {
  console.error('Login failed:', result.message);
  console.error('Error code:', result.error);
}

Response Structure

// Success response
{
  success: true,
  message: "Login successful",
  data: {
    user: {
      id: "user-uuid",
      email: "user@example.com",
      username: "username", 
      role: "user"
    }
  }
}

// Error response
{
  success: false,
  error: "LOGIN_FAILED",
  message: "Invalid email or password"
}

Common Error Codes

LOGIN_FAILEDInvalid credentials
NETWORK_ERRORConnection issues
INTERNAL_SERVER_ERRORServer error

NOTE : If the user ain't verified then it send an verify email automatically u can also use resendVerificationEmail(email) method ALSO .

signOut()

The signOut method logs out users and clears all authentication tokens and cookies. It requires an active session (user must be authenticated).

Usage

import { signOut } from 'easyauth';

// Sign out current user
const result = await signOut();

if (result.success) {
  console.log('Logout successful');
  // User is now logged out
  // All tokens and cookies are cleared
} else {
  console.error('Logout failed:', result.message);
}

What happens during signOut

Server-side session invalidation
Clears HTTP-only cookies
Clears session manager state
Removes access and refresh tokens

signUp()

The signUp method registers new users with email, password, and username. You can optionally enable email verification during registration.

Basic Usage

import { signUp } from 'easyauth';

// Basic registration (no email verification)
const result = await signUp(
  'user@example.com', 
  'password123', 
  'username'
);

if (result.success) {
  console.log('Registration successful!');
  console.log('User data:', result.data);
} else {
  console.error('Registration failed:', result.message);
}

With Email Verification

// Registration with email verification
const result = await signUp(
  'user@example.com',
  'password123',
  'username',
  {
    sendVerificationEmail: true
  }
);

if (result.success) {
  console.log('Registration successful!');
  console.log('Verification email sent to user');
}

Parameters

ParameterTypeRequiredDescription
emailstringYesUser's email address
passwordstringYesUser's password
usernamestringYesUnique username
emailConfigobjectNoEmail verification settings

forgotPassword()

The forgotPassword method sends a password reset email to the specified user. The server handles the reset flow with its own reset pages.

Usage

import { forgotPassword } from 'easyauth';

// Send password reset email
const result = await forgotPassword('user@example.com');

if (result.success) {
  console.log('Password reset email sent!');
  console.log(result.message);
} else {
  console.error('Failed to send reset email:', result.message);
}

How it works

User receives email with reset link
Link opens server's built-in reset page
User enters new password
Password is updated automatically

Error Handling

All authentication methods can throw errors that should be properly handled in your application.

import { signIn, AuthError } from 'easyauth';

try {
  await signIn({
    email: 'user@example.com',
    password: 'wrongpassword'
  });
} catch (error) {
  if (error instanceof AuthError) {
    switch (error.code) {
      case 'invalid_credentials':
        console.error('Invalid email or password');
        break;
      case 'user_not_found':
        console.error('User does not exist');
        break;
      case 'too_many_attempts':
        console.error('Too many failed attempts. Try again later.');
        break;
      default:
        console.error('Authentication failed:', error.message);
    }
  } else {
    console.error('Network error:', error);
  }
}

Next Steps

Now that you understand authentication methods, explore other SDK features: