Manage user authentication with login, logout, registration, and OAuth integration
The Inspatial Cloud Client SDK provides comprehensive authentication methods through the auth group, including email/password authentication, Google OAuth, and password reset functionality.
Create a new user account with email and password.
const sessionData = await client.auth.registerAccount({ email: '[email protected]', password: 'securePassword123', firstName: 'John', lastName: 'Doe'});// User is automatically logged in after registrationconsole.log('Account created for:', sessionData.email);
// Redirect user to Google OAuth flowawait client.auth.signInWithGoogle({ redirectTo: 'https://yourapp.com/dashboard', csrfToken: 'your-csrf-token' // Optional, stored in localStorage});
The user will be redirected to Google for authentication. After successful authentication, they’ll be redirected back to the redirectTo URL.
// Redirect user to Google OAuth flow for registrationawait client.auth.signupWithGoogle({ redirectTo: 'https://yourapp.com/welcome', csrfToken: 'your-csrf-token' // Optional});
Here’s a complete example showing a typical authentication flow:
import { InCloudClient } from '@inspatial/cloud-client';const client = new InCloudClient('https://api.yourapp.com');// Check if user is already authenticatedconst existingSession = await client.auth.authCheck();if (existingSession) { console.log('Welcome back,', existingSession.firstName);} else { // User is not authenticated, show login form try { const session = await client.auth.login( '[email protected]', 'password123' ); console.log('Login successful:', session.email); } catch (error) { console.error('Login failed:', error); }}// Later, when user wants to logoutawait client.auth.logout();