asgardeoExpressAuth()
asgardeoExpressAuth() returns an Express router that adds the sign-in and sign-out routes to your app. It is a static method of AsgardeoExpressClient.
static asgardeoExpressAuth(
onSignIn: (res: express.Response, response: TokenResponse, req: express.Request) => void,
onSignOut: (res: express.Response, req: express.Request) => void,
onError: (res: express.Response, exception: AsgardeoAuthException, req: express.Request) => void
): express.Router
Call it after AsgardeoExpressClient.getInstance(config), or it throws an error. Add a cookie parser before it, because the routes read the session cookie from req.cookies.
import express from "express";
import cookieParser from "cookie-parser";
import { AsgardeoExpressClient } from "@asgardeo/express";
const app = express();
app.use(cookieParser());
AsgardeoExpressClient.getInstance({
appURL: "http://localhost:3000",
baseUrl: "https://localhost:9443",
clientId: "<your-app-client-id>",
clientSecret: "<your-app-client-secret>"
});
app.use(
AsgardeoExpressClient.asgardeoExpressAuth(
(res, tokenResponse) => {
res.redirect("/profile");
},
(res) => {
res.redirect("/");
},
(res, error) => {
res.status(500).send(error.message);
}
)
);
Routes¶
The router adds two GET routes. Change their paths with the loginPath and logoutPath options.
| Route | Default path | What it does |
|---|---|---|
| Sign-in | /login |
Starts the sign-in flow and handles the redirect back from WSO2 Identity Server. |
| Sign-out | /logout |
Starts the sign-out flow and handles the redirect back from WSO2 Identity Server. |
Sign-in route¶
The sign-in route is also the redirect URL of the application, so it handles both steps of sign-in:
- When the user opens it, the SDK sets the
ASGARDEO_SESSION_IDcookie and redirects them to WSO2 Identity Server. If the user already has a valid session, the SDK callsonSignIninstead. - When WSO2 Identity Server redirects the user back with
codeandstatequery parameters, the SDK exchanges the code for tokens, stores them against the session ID, and callsonSignIn.
If WSO2 Identity Server redirects back with an error query parameter, for example because the user cancelled sign-in, or if getting the tokens fails, the SDK calls onError.
Sign-out route¶
- When the user opens it, the SDK gets the WSO2 Identity Server sign-out URL for the session, clears the
ASGARDEO_SESSION_IDcookie, and redirects the user to WSO2 Identity Server. If the request has noASGARDEO_SESSION_IDcookie, the SDK callsonErrorinstead. - When WSO2 Identity Server redirects the user back with
state=sign_out_success, the SDK callsonSignOut.
The sign-out route also removes the user's tokens from the SDK's store.
Callbacks¶
Each callback receives the Express response, and the request as its last argument. Use the response to finish the request, for example with res.redirect() or res.send(). If a callback doesn't send a response, the request is left hanging.
| Callback | Signature | Called when |
|---|---|---|
onSignIn |
(res: express.Response, response: TokenResponse, req: express.Request) => void |
The user is signed in. response holds the tokens. See TokenResponse. |
onSignOut |
(res: express.Response, req: express.Request) => void |
WSO2 Identity Server redirects the user back to the sign-out route after sign-out. |
onError |
(res: express.Response, exception: AsgardeoAuthException, req: express.Request) => void |
Sign-in or sign-out fails. exception has code, name and message fields. |
The client on the request¶
The router also adds the client to every request that passes through it, as req.asgardeoAuth and res.asgardeoAuth. Use it in routes that come after the router: