remix-auth
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -5,5 +5,32 @@ import { Request, Response, SessionStorage } from "@remix-run/node"; | ||
} | ||
/** | ||
* Extra options for the authenticator. | ||
*/ | ||
export interface AuthenticatorOptions { | ||
sessionKey?: string; | ||
} | ||
/** | ||
* Extra information from the Authenticator to the strategy | ||
*/ | ||
export interface StrategyOptions { | ||
sessionKey: string; | ||
} | ||
export interface Strategy<User> { | ||
/** | ||
* The name of the strategy. | ||
* This will be used by the Authenticator to identify and retrieve the | ||
* strategy. | ||
*/ | ||
name: string; | ||
authenticate(request: Request, sessionStorage: SessionStorage, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
/** | ||
* The authentication flow of the strategy. | ||
* | ||
* This method receives the Request to authenticator and the session storage | ||
* to use from the Authenticator. It may receive a custom callback. | ||
* | ||
* At the end of the flow, it will return a Response be use used by the | ||
* application. This response could be a redirect or a custom one returned by | ||
* the optional callback. | ||
*/ | ||
authenticate(request: Request, sessionStorage: SessionStorage, options: StrategyOptions, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
} | ||
@@ -14,8 +41,83 @@ export declare class AuthorizationError extends Error { | ||
private sessionStorage; | ||
/** | ||
* A map of the configured strategies, the key is the name of the strategy | ||
* @private | ||
*/ | ||
private strategies; | ||
constructor(sessionStorage: SessionStorage); | ||
readonly sessionKey: string; | ||
/** | ||
* Create a new instance of the Authenticator. | ||
* | ||
* It receives a instance of the SessionStorage. This session storage could | ||
* be created using any method exported by Remix, this includes: | ||
* - `createSessionStorage` | ||
* - `createFileSystemSessionStorage` | ||
* - `createCookieSessionStorage` | ||
* - `createMemorySessionStorage` | ||
* | ||
* It optionally receives an object with extra options. The supported options | ||
* are: | ||
* - `sessionKey`: The key used to store and red the user in the session storage. | ||
* @example | ||
* import { sessionStorage } from "./session.server"; | ||
* let authenticator = new Authenticator(sessionStorage); | ||
* @example | ||
* import { sessionStorage } from "./session.server"; | ||
* let authenticator = new Authenticator(sessionStorage, { | ||
* sessionKey: "token", | ||
* }); | ||
*/ | ||
constructor(sessionStorage: SessionStorage, options?: AuthenticatorOptions); | ||
/** | ||
* Call this method with the Strategy, the optional name allows you to setup | ||
* the same strategy multiple times with different names. | ||
* It returns the Authenticator instance for concatenation. | ||
* @example | ||
* authenticator | ||
* .use(new SomeStrategy({}, (user) => Promise.resolve(user))) | ||
* .use(new SomeStrategy({}, (user) => Promise.resolve(user)), "another"); | ||
*/ | ||
use(strategy: Strategy<User>, name?: string): Authenticator; | ||
/** | ||
* Call this method with the name of the strategy you want to remove. | ||
* It returns the Authenticator instance for concatenation. | ||
* @example | ||
* authenticator.unuse("another").unuse("some"); | ||
*/ | ||
unuse(name: string): Authenticator; | ||
/** | ||
* Call this to authenticate a request using some strategy. You pass the name | ||
* of the strategy you want to use and the request to authenticate. | ||
* The optional callback allows you to do something with the user object | ||
* before returning a new Response. In case it's not provided the strategy | ||
* will return a new Response and set the user to the session. | ||
* @example | ||
* let action: ActionFunction = ({ request }) => { | ||
* return authenticator.authenticate("some", request); | ||
* }; | ||
* @example | ||
* let action: ActionFunction = ({ request }) => { | ||
* return authenticator.authenticate("some", request, async user => { | ||
* let session = await getSession(request.headers.get("Cookie")); | ||
* session.set(authenticator.key, user); | ||
* return redirect("/private", { | ||
* "Set-Cookie": await commitSession(session), | ||
* }); | ||
* }); | ||
* }; | ||
*/ | ||
authenticate(strategy: string, request: Request, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
/** | ||
* Call this to check if the user is authenticated. It will return a Promise | ||
* with the user object or null, you can use this to check if the user is | ||
* logged-in or not withour triggering the whole authentication flow. | ||
* @example | ||
* let loader: LoaderFunction = async ({ request }) => { | ||
* let user = await authenticator.isAuthenticated(request); | ||
* if (!user) return redirect("/login"); | ||
* // do something with the user | ||
* return json(data); | ||
* } | ||
*/ | ||
isAuthenticated(request: Request): Promise<User | null>; | ||
} |
@@ -41,6 +41,8 @@ var __create = Object.create; | ||
var Authenticator = class { | ||
constructor(sessionStorage) { | ||
constructor(sessionStorage, options = {}) { | ||
this.sessionStorage = sessionStorage; | ||
this.sessionKey = options.sessionKey || "user"; | ||
} | ||
strategies = new Map(); | ||
sessionKey; | ||
use(strategy, name) { | ||
@@ -58,10 +60,13 @@ this.strategies.set(name ?? strategy.name, strategy); | ||
throw new Error(`Strategy ${strategy} not found.`); | ||
let options = { | ||
sessionKey: this.sessionKey | ||
}; | ||
if (!callback) { | ||
return strategyObj.authenticate(request.clone(), this.sessionStorage); | ||
return strategyObj.authenticate(request.clone(), this.sessionStorage, options); | ||
} | ||
return strategyObj.authenticate(request.clone(), this.sessionStorage, callback); | ||
return strategyObj.authenticate(request.clone(), this.sessionStorage, options, callback); | ||
} | ||
async isAuthenticated(request) { | ||
let session = await this.sessionStorage.getSession(request.clone().headers.get("Cookie")); | ||
let user = session.get("user"); | ||
let session = await this.sessionStorage.getSession(request.headers.get("Cookie")); | ||
let user = session.get(this.sessionKey); | ||
if (user) | ||
@@ -96,6 +101,6 @@ return user; | ||
} | ||
async authenticate(request, sessionStorage, callback) { | ||
async authenticate(request, sessionStorage, options, callback) { | ||
let url = new URL(request.url); | ||
let session = await sessionStorage.getSession(request.headers.get("Cookie")); | ||
let user = session.get("user") ?? null; | ||
let user = session.get(options.sessionKey) ?? null; | ||
if (user) | ||
@@ -125,3 +130,3 @@ return callback ? callback(user) : (0, import_node.redirect)("/"); | ||
return callback(user); | ||
session.set("user", user); | ||
session.set(options.sessionKey, user); | ||
let cookie = await sessionStorage.commitSession(session); | ||
@@ -250,3 +255,3 @@ return (0, import_node.redirect)("/", { headers: { "Set-Cookie": cookie } }); | ||
} | ||
async authenticate(request, _sessionStorage, callback) { | ||
async authenticate(request, _sessionStorage, _options, callback) { | ||
if (!callback) { | ||
@@ -297,7 +302,7 @@ throw new TypeError("The authenticate callback on BasicStrategy is required."); | ||
} | ||
async authenticate(request, sessionStorage, callback) { | ||
async authenticate(request, sessionStorage, options, callback) { | ||
if (!callback) { | ||
throw new TypeError("The authenticate callback on CustomStrategy is required."); | ||
} | ||
return callback(await this.verify(request, sessionStorage)); | ||
return callback(await this.verify(request, sessionStorage, options)); | ||
} | ||
@@ -323,3 +328,3 @@ }; | ||
} | ||
async authenticate(request, sessionStorage, callback) { | ||
async authenticate(request, sessionStorage, options, callback) { | ||
if (new URL(request.url).pathname !== this.loginURL) { | ||
@@ -349,3 +354,3 @@ throw new AuthorizationError("The authenticate method with LocalStrategy can only be used on the login URL."); | ||
return callback(user); | ||
session.set("user", user); | ||
session.set(options.sessionKey, user); | ||
let cookie = await sessionStorage.commitSession(session); | ||
@@ -368,3 +373,3 @@ return (0, import_node4.redirect)("/", { headers: { "Set-Cookie": cookie } }); | ||
authenticate() { | ||
return Promise.resolve(this.response); | ||
return Promise.resolve(this.response.clone()); | ||
} | ||
@@ -371,0 +376,0 @@ }; |
import { Request, Response, SessionStorage } from "@remix-run/node"; | ||
import { AuthenticateCallback, Strategy } from "../authenticator"; | ||
import { AuthenticateCallback, Strategy, StrategyOptions } from "../authenticator"; | ||
export interface BasicStrategyOptions { | ||
@@ -39,5 +39,5 @@ realm?: string; | ||
constructor(verify: BasicStrategyVerifyCallback<User>); | ||
authenticate(request: Request, _sessionStorage: SessionStorage, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
authenticate(request: Request, _sessionStorage: SessionStorage, _options: StrategyOptions, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
private raise; | ||
private challange; | ||
} |
import { Request, Response, SessionStorage } from "@remix-run/node"; | ||
import { AuthenticateCallback, Strategy } from "../authenticator"; | ||
import { AuthenticateCallback, Strategy, StrategyOptions } from "../authenticator"; | ||
export interface CustomStrategyVerifyCallback<User> { | ||
(request: Request, sessionStorage: SessionStorage): Promise<User>; | ||
(request: Request, sessionStorage: SessionStorage, options: StrategyOptions): Promise<User>; | ||
} | ||
@@ -24,3 +24,3 @@ /** | ||
constructor(verify: CustomStrategyVerifyCallback<User>); | ||
authenticate(request: Request, sessionStorage: SessionStorage, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
authenticate(request: Request, sessionStorage: SessionStorage, options: StrategyOptions, callback: AuthenticateCallback<User>): Promise<Response>; | ||
} |
import { Request, Response, SessionStorage } from "@remix-run/node"; | ||
import { AuthenticateCallback, Strategy } from "../authenticator"; | ||
import { AuthenticateCallback, Strategy, StrategyOptions } from "../authenticator"; | ||
export interface LocalStrategyOptions { | ||
@@ -20,3 +20,3 @@ loginURL: string; | ||
constructor(options: LocalStrategyOptions, verify: LocalStrategyVerifyCallback<User>); | ||
authenticate(request: Request, sessionStorage: SessionStorage, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
authenticate(request: Request, sessionStorage: SessionStorage, options: StrategyOptions, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
} |
import { Request, Response, SessionStorage } from "@remix-run/node"; | ||
import { AuthenticateCallback, Strategy } from "../authenticator"; | ||
import { AuthenticateCallback, Strategy, StrategyOptions } from "../authenticator"; | ||
export interface OAuth2Profile { | ||
@@ -82,3 +82,3 @@ provider: string; | ||
constructor(options: OAuth2StrategyOptions, verify: OAuth2StrategyVerifyCallback<User, Profile, ExtraParams>); | ||
authenticate(request: Request, sessionStorage: SessionStorage, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
authenticate(request: Request, sessionStorage: SessionStorage, options: StrategyOptions, callback?: AuthenticateCallback<User>): Promise<Response>; | ||
/** | ||
@@ -85,0 +85,0 @@ * Retrieve user profile from service provider. |
{ | ||
"name": "remix-auth", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Simple Authentication for Remix", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -93,2 +93,3 @@ ![](/assets/header.png) | ||
let session = await getSession(request.headers.get("Cookie")); | ||
session.set(authenticator.sessionKey, user); | ||
return redirect("/dashboard", { | ||
@@ -95,0 +96,0 @@ headers: { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
46479
1107
119
2