- Provides a method for helpers for oauth authentication.
- Nodejs v18 or higher
npm install @swit-api/oauth
Create an instance by putting options in the Oauth class constructor.
import { Oauth } from '@swit-api/oauth';
const clientId = process.env.CLIENT_ID as string;
const clientSecret = process.env.CLIENT_SECRET as string;
const redirectUri = process.env.REDIRECT_URI as string;
const scope = [
'channel:read',
'message:read',
'channel:write',
'idea:read',
'idea:write',
'message:write'
];
const oauth = new Oauth({
clientId,
clientSecret,
redirectUri,
state: '',
scope
});
Get oauth authentication page url via getAuthorizeUrl method.
oauth.getAuthorizeUrl()
After authentication, you receive the code parameter that is passed to the redirect url and pass it to the function below to get a token.
const code = 'Code passed after authentication';
oauth.getTokenByAuthorizationCode(code);
Get a renewed token with a previously received refresh token.
const refreshToken = 'refresh token';
oauth.getTokenByRefreshToken(refreshToken);