Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom logout function #224

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Session,
SessionStorage,
} from "@remix-run/server-runtime";
import { AuthenticateOptions, Strategy } from "./strategy";
import { AuthenticateOptions, LogoutOptions, Strategy } from "./strategy";

export interface AuthenticateCallback<User> {
(user: User): Promise<Response>;
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Authenticator<User = unknown> {
* .use(new SomeStrategy({}, (user) => Promise.resolve(user)))
* .use(new SomeStrategy({}, (user) => Promise.resolve(user)), "another");
*/
use(strategy: Strategy<User, never>, name?: string): Authenticator {
use(strategy: Strategy<User, never>, name?: string): Authenticator<User> {
this.strategies.set(name ?? strategy.name, strategy);
return this;
}
Expand All @@ -88,7 +88,7 @@ export class Authenticator<User = unknown> {
* @example
* authenticator.unuse("another").unuse("some");
*/
unuse(name: string): Authenticator {
unuse(name: string): Authenticator<User> {
this.strategies.delete(name);
return this;
}
Expand Down Expand Up @@ -237,12 +237,21 @@ export class Authenticator<User = unknown> {
*/
async logout(
request: Request | Session,
options: { redirectTo: string }
options: LogoutOptions
): Promise<never> {
let session = isSession(request)
? request
: await this.sessionStorage.getSession(request.headers.get("Cookie"));

let strategyName = session.get(this.sessionStrategyKey);
let strategy = this.strategies.get(strategyName);

let user: User | null = session.get(this.sessionKey) ?? null;

if (user !== null && typeof strategy?.logout === "function") {
return strategy.logout(user, request, this.sessionStorage, options);
}

throw redirect(options.redirectTo, {
headers: {
"Set-Cookie": await this.sessionStorage.destroySession(session),
Expand Down
12 changes: 12 additions & 0 deletions src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AppLoadContext,
json,
redirect,
Session,
SessionStorage,
} from "@remix-run/server-runtime";
import { AuthorizationError } from "./error";
Expand Down Expand Up @@ -51,6 +52,10 @@ export interface AuthenticateOptions {
context?: AppLoadContext;
}

export interface LogoutOptions {
redirectTo: string;
}

/**
* A function which will be called to find the user using the information the
* strategy got from the request.
Expand Down Expand Up @@ -168,4 +173,11 @@ export abstract class Strategy<User, VerifyOptions> {
headers: { "Set-Cookie": await sessionStorage.commitSession(session) },
});
}

public logout?(
user: User,
request: Request | Session,
sessionStorage: SessionStorage,
options: LogoutOptions
): Promise<never>;
}