guardianAuth()
The guardianAuth()
function is the primary entry point for setting up Svelte Guardian in your SvelteKit application. It configures authentication providers, security settings, and creates the necessary middleware for handling authentication.
Syntax
Parameters
config
- A GuardianAuthConfig object containing all configuration options.
Return Value
Returns an object with the following properties:
handle
: A SvelteKit handle function to be used in your hooks.server.ts filesignIn
: A function for programmatically signing in userssignOut
: A function for programmatically signing out usersmiddleware
: A function that can be composed with other SvelteKit middleware
Example
Configuration Options
For detailed information about all available configuration options, see the Configuration Options documentation.
signIn()
The signIn()
function is used to authenticate users with different providers. It can be called from both server-side and client-side code.
Server-Side Usage
Syntax
Parameters
provider
: A string representing the authentication provider to use. Available values:"credentials"
: Email and password authentication"google"
: Google OAuth authentication"github"
: GitHub OAuth authentication"facebook"
: Facebook OAuth authentication"twitter"
: Twitter OAuth authentication- Any other OAuth provider configured in your application
options
: A SignInOptions object containing:- For Credentials provider:
email
: User emailpassword
: User passwordisRegistration
: Boolean indicating if this is a new user registrationredirect
: Whether to redirect after sign in (default: true)callbackUrl
: Where to redirect after successful sign incookies
: SvelteKit cookies object (required for server-side)
- For OAuth providers:
redirect
: Whether to redirect after sign in (default: true)callbackUrl
: Where to redirect after successful sign incookies
: SvelteKit cookies object (required for server-side)
- For Credentials provider:
Return Value
Returns a SignInResult object with:
ok
: Boolean indicating successerror
: Error message if sign in failedurl
: Redirect URL if applicableuser
: User object if sign in succeeded
Example (Server Side)
Client-Side Usage
Syntax
Example (Client Side)
OAuth Flow
When using OAuth providers, the signIn()
function initiates an OAuth flow:
signIn('google')
redirects the user to the Google authorization page- After the user authorizes your app, Google redirects back to your application
- Svelte Guardian handles the callback, verifies the tokens, and creates a session
- The user is redirected to the callbackUrl or the default page
Registration Flow
For new user registration with the credentials provider:
signOut()
The signOut()
function is used to end a user’s session and sign them out of your application. It can be called from both server-side and client-side code.
Server-Side Usage
Syntax
Parameters
options
: A SignOutOptions object containing:redirect
: Whether to redirect after sign out (default: true)callbackUrl
: Where to redirect after successful sign outcookies
: SvelteKit cookies object (required for server-side)
Return Value
Returns a SignOutResult object with:
ok
: Boolean indicating successerror
: Error message if sign out failedurl
: Redirect URL if applicable
Example (Server Side)
Client-Side Usage
Syntax
Example (Client Side)
Session Cleanup
When a user signs out, Svelte Guardian performs the following cleanup tasks:
- Invalidates the current session
- Removes session cookies from the browser
- Updates session records in the database (for database sessions)
- Triggers the
afterSignOut
event callback if configured
Sign Out All Sessions
To sign out a user from all their active sessions:
createUser()
The createUser()
function is used to programmatically create new users in your application. It’s particularly useful for admin functionality, seeding databases, or implementing custom registration flows.
Syntax
Parameters
userData
: A CreateUserData object containing:email
: User’s email address (required)password
: User’s password (required for credentials provider)name
: User’s display name (optional)role
: User’s role (optional, defaults to ‘user’)emailVerified
: Whether the email is already verified (optional)- Additional custom fields as needed
options
: A CreateUserOptions object containing:sendVerificationEmail
: Whether to send a verification email (default: false)skipPasswordHashing
: Whether the password is already hashed (default: false)skipValidation
: Whether to skip validation rules (default: false)provider
: Which auth provider to associate with the user (default: ‘credentials’)
Return Value
Returns a User object representing the newly created user.
Example
Basic Usage
With Email Verification
With Pre-Hashed Password
With Custom Fields
Error Handling
The createUser()
function throws errors that should be caught and handled appropriately:
Events
Creating a user triggers the following events if configured:
onUserCreation
: Called when a user is successfully createdafterRegistration
: Called after a user is created via registration