RaisFastRaisFast
JavaScript SDK

Auth & Users

Login, register, OAuth, SMS, password reset, email verification, and user profile management.

The client.auth module handles all authentication flows. Tokens are auto-stored and auto-refreshed on 401 responses.

Email & Password

Login

const result = await client.auth.login("admin@raisfast.dev", "admin123");
console.log(result.access_token);
console.log(result.refresh_token);
console.log(result.user);

Register

await client.auth.register({
  email: "user@example.com",
  password: "strongpassword",
  username: "newuser",
});

Logout

await client.auth.logout();

Get Current User

const me = await client.auth.getMe();

Check Auth State

client.auth.isAuthenticated;  // boolean
client.auth.token;            // string | null
client.auth.user;             // User | null

Auto-Refresh

When a request returns 401 and a refresh token exists, the SDK automatically calls /auth/refresh and retries the original request. If refresh fails, the auth store is cleared.

OAuth

List Providers

const providers = await client.auth.listOAuthProviders();
// [{ name: "github", display_name: "GitHub", ... }, ...]

Get Redirect URL

const url = client.auth.getOAuthRedirectURL("github");
// Redirect user to this URL for authorization
window.location.href = url;

Handle Callback

After the user is redirected back with a code:

const result = await client.auth.authWithOAuth({
  provider: "github",
  code: "oauth_code_from_callback",
  redirect_url: "https://yourapp.com/callback",
});
// result.access_token, result.user

Manage Bindings

// List bound providers
const bindings = await client.auth.listOAuthBindings();

// Unbind a provider
await client.auth.unbindOAuth("github");

SMS (Phone)

Send Verification Code

await client.auth.sendSmsCode("+8613800138000", "login");

Verify and Login

const result = await client.auth.verifySms({
  phone: "+8613800138000",
  code: "123456",
  purpose: "login",
});

Bind Phone

await client.auth.bindPhone({
  phone: "+8613800138000",
  code: "123456",
});

Password Reset

// Step 1: Request reset (sends email with token)
await client.auth.requestPasswordReset("user@example.com");

// Step 2: Confirm reset with token
await client.auth.confirmPasswordReset({
  token: "reset_token_from_email",
  new_password: "newStrongPassword",
});

Admin Set Password

await client.auth.setPassword({
  email: "user@example.com",
  new_password: "temporaryPassword",
});

Email Verification

// Request verification email
await client.auth.resendVerification("user@example.com");

// Verify with token
await client.auth.verifyEmail({ token: "verification_token" });

Auth Config

Get the server's auth configuration (enabled methods, OAuth providers, etc.):

const config = await client.auth.getConfig();

Credential Management

// List all credentials (email, phone, OAuth bindings)
const credentials = await client.auth.listCredentials();

// Bind an email
await client.auth.bindEmail({
  email: "user@example.com",
  password: "password",
});

// Remove a credential
await client.auth.deleteCredential(credentialId);

User Profile

Get Profile

const me = await client.users.getMe();

Update Profile

const updated = await client.users.updateMe({
  username: "newname",
  bio: "Full-stack developer",
  website: "https://example.com",
  avatar: "media_id",
  social_links: {
    github: "https://github.com/user",
    twitter: "https://twitter.com/user",
  },
});

Change Password

await client.auth.changePassword({
  old_password: "currentPassword",
  new_password: "newStrongPassword",
});

View Another User

const user = await client.users.getUser("user_id");

List Users

const result = await client.users.listUsers(1, 20);

Auth State Changes

const unsubscribe = client.authStore.onChange((token, user) => {
  if (token) {
    console.log("Logged in as", user?.email);
  } else {
    console.log("Logged out");
  }
}, true); // fire immediately with current state

// Later: stop listening
unsubscribe();

On this page