Skip to main content

Login & Logout

Implement login and logout functionality in your application.

Triggering Login

The recommended way to log users in is by redirecting them to the SyAuth Universal Login page.

Client Component

Use the useSyAuth hook to trigger login:

'use client';
import { useSyAuth } from '@syauth/nextjs';

export function LoginButton() {
const { loginWithRedirect, isLoading } = useSyAuth();

return (
<button onClick={() => loginWithRedirect()}>
Sign In
</button>
);
}

Custom Parameters

You can override default settings:

loginWithRedirect({
redirectUri: 'https://myapp.com/callback',
scope: 'openid profile email offline_access',
});

Handling the Callback

After login, SyAuth redirects the user back to your application with an authorization code.

The SDK handles the code exchange automatically if you setup the AuthCallback component.

// src/app/auth/callback/page.tsx
'use client';
import { useOAuthCallback } from '@syauth/nextjs';
// ... standard callback implementation (see Quickstart)

Logout

const { logout } = useSyAuth();
// ...
<button onClick={() => logout()}>Sign Out</button>

Protected Routes

Middleware Approach:

// src/middleware.ts
export default withAuth({
protectedRoutes: ['/dashboard'],
});

Component Approach:

const { isAuthenticated } = useSyAuth();
if (!isAuthenticated) return <Login />;