Function oauth2

  • A basic handler for OAuth2, except the user does most of the work.

    Returns

    An auth handler.

    Example

    Github example (will not work without full setup)

    const clientId = "<CLIENT_ID>";
    const clientSecret = "<CLIENT_ID>";
    authentication.addAuth(
    "github",
    oauth2({
    oauth2Handler: async (req) => {
    const { data: at_data } = await axios({
    url: `https://github.com/login/oauth/access_token?client_id=${clientId}&client_secret=${clientSecret}&code=${req.query.code}`,
    method: "POST",
    headers: { Accept: "application/json" },
    });
    const profile = await axios.get("https://api.github.com/user", {
    headers: {
    Authorization: "token " + at_data.access_token,
    Accept: "application/vnd.github+json",
    },
    });
    return {
    username: profile.data.login,
    bio: profile.data.bio,
    photos: [
    {
    value:
    "https://github.com/" + profile.data.login + ".png",
    },
    ],
    _raw: profile.data,
    };
    },
    oauth2AccountHandler: async (profile, req) => {
    // Handle either sign in or sign up here.
    // profile is what was returned from oauth2Handler.
    },
    })
    );

    app.get("/oauth/github", (req, res) => {
    res.redirect(
    `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=http://localhost:3000/oauth/github/redirect`
    );
    });

    app.get(
    "/oauth/github/redirect",
    authentication.authenticate("github"),
    function (req, res) {
    res.redirect("/");
    }
    );

    Parameters

    Returns AuthHandler

Generated using TypeDoc