koa-passport-zalo v1.0.0
passport-zalo
Passport strategy for authenticating with Facebook using the OAuth 2.0 API.
This module lets you authenticate using Zalo in your Node.js applications. By plugging into Passport, Zalo authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware Zalo authentication strategy for Passport and Node.js
Install
npm install koa-passport-zalo
Usage
Create an Application
Before using passport-zalo
, you must register an application with Zalo. If you have not already done so, a new application can be created at Zalo Developers. Your application will be issued an app ID and app secret, which need to be provided to the strategy. You will also need to configure a redirect URI which matches the route in your application.
Configure Strategy
The Zalo authentication strategy authenticates users using a Zalo account and OAuth 2.0 tokens. The app ID and secret obtained when creating an application are supplied as options when creating the strategy. The strategy also requires a verify callback, which receives the access token and optional refresh token, as well as profile which contains the authenticated user's Zalo profile. The verify callback must call done providing a user to complete authentication.
import passport from "koa-passport";
import { Strategy as ZaloStrategy } from "zalo-passport";
passport.use(
new ZaloStrategy(
{
clientID: "ZALO_CLIENT_ID",
clientSecret: "ZALO_CLIENT_SECRET",
callbackURL: "ZALO_CALLBACK_URL",
},
async (accessToken, refreshToken, profile, done) => {
try {
const member = await Member.findOrCreate())// Find or Create your member by profile
return done(null, member);
} catch (error) {
return done(error, null);
}
}
)
);
Authenticate Requests
Use passport.authenticate(), specifying the 'zalo' strategy, to authenticate requests. For example, as route middleware in an Koa application:
router.get("/zalo", passport.authenticate("zalo"));
router.get("/zalo/callback", async (ctx) =>
passport.authenticate("zalo", (err, token) => {
ctx.body = { err, token };
})(ctx)
);
Examples
Illustrates how to use the Zalo strategy within an Koa application.
FAQ
License
Copyright (c) 2022 Lý Thành Đạt
2 years ago