@inertiapixel/nextjs-auth
Version:
Authentication system for Next.js. Supports credentials and social login, JWT token management, and lifecycle hooks — designed to integrate with nodejs-auth for full-stack MERN apps.
92 lines (91 loc) • 2.57 kB
TypeScript
import { ReactNode } from "react";
export interface DecodedToken {
id: string;
email: string;
name: string;
role?: string;
avatar?: string;
}
export interface User<T = unknown> {
id: string;
name: string;
email: string;
role?: string;
avatar?: string;
extra?: T;
}
export type LoginPayload = {
provider: 'credentials';
email: string;
password: string;
} | {
provider: 'otp';
email: string;
otp: string;
};
export interface AuthResponse {
isAuthenticated: boolean;
message: string;
accessToken?: string;
user?: User;
}
export interface AuthContextType {
user: User | null;
isAuthenticated: boolean;
loading: boolean;
login: (credentials: LoginPayload) => Promise<void>;
socialLogin: (payload: SocialProvider) => void;
logout: () => void;
loginError: Record<string, unknown> | null;
}
export interface OAuthProviderConfig {
clientId: string;
}
export interface SocialProviderConfig {
provider: SocialProvider;
clientId: string;
}
export declare const SOCIAL_PROVIDERS: readonly ["google", "facebook", "linkedin"];
export type SocialProvider = (typeof SOCIAL_PROVIDERS)[number];
export interface AuthProviderProps {
children: ReactNode;
config: {
apiBaseUrl?: string;
apiEndpoints?: {
login: string;
refresh: string;
register: string;
logout: string;
};
tokenKey?: string;
socialProviders?: SocialProviderConfig[];
/**
* Default redirect destination after login.
* If `redirectTo` is provided, it overrides the `redirectTo` query param.
*/
redirectTo?: string;
/**
* Optional redirect if already authenticated (e.g., trying to visit `/login`)
* Defaults to `/` (home page) if not specified.
*/
redirectIfAuthenticated?: string;
/**
* Optional redirect if not authenticated and accessing a protected route.
* Defaults to `/login` if not specified.
*/
redirectIfNotAuthenticated?: string;
/**
* Optional redirect destination after logout.
* Defaults to `/login` if not specified.
*/
redirectAfterLogout?: string;
onLoginSuccess?: (user: User | null) => void;
onLoginFail?: (error: string) => void;
onLogout?: () => void;
};
}
export type OTPPayload = {
phone: string;
otp: string;
};
export type OAuthProvider = 'credentials' | 'otp' | 'magiclink';