Diyorbek
Back to notes
13,558 views

Authentication Context

nextjsreact

A tiny auth context pattern: provide the user once, read it anywhere with a hook.

const AuthContext = createContext<User | null>(null);
export const useUser = () => useContext(AuthContext);

Wrap the app in the provider and hydrate from your session on mount.