2022-08-29 18:34:46 +00:00
|
|
|
import Cookies from 'cookies';
|
2022-08-29 18:09:28 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
2022-08-29 18:34:46 +00:00
|
|
|
import {
|
|
|
|
CLIENT_ID,
|
|
|
|
COOKIE_KEYS,
|
|
|
|
OAUTH_URL,
|
2022-09-09 18:50:28 +00:00
|
|
|
REDIRECT_URL,
|
2022-08-29 18:34:46 +00:00
|
|
|
} from '../../lib/constants';
|
|
|
|
import { encrypt, generateString } from '../../lib/utils/crypto';
|
|
|
|
|
2022-09-11 09:31:09 +00:00
|
|
|
const inProd = process.env.NODE_ENV === 'production';
|
|
|
|
|
2022-08-29 18:09:28 +00:00
|
|
|
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
2022-08-29 18:34:46 +00:00
|
|
|
const stateToken = generateString(16);
|
|
|
|
const state = encrypt(
|
|
|
|
JSON.stringify({
|
2022-09-09 18:50:28 +00:00
|
|
|
redirect_uri: REDIRECT_URL,
|
2022-08-29 18:34:46 +00:00
|
|
|
state: stateToken,
|
|
|
|
})
|
|
|
|
);
|
2022-08-30 18:08:54 +00:00
|
|
|
|
2022-08-29 18:09:28 +00:00
|
|
|
const params = new URLSearchParams({
|
|
|
|
client_id: CLIENT_ID,
|
|
|
|
response_type: 'code',
|
2022-09-09 18:50:28 +00:00
|
|
|
redirect_uri: REDIRECT_URL,
|
2022-08-29 18:09:28 +00:00
|
|
|
scope: 'management',
|
2022-08-29 18:34:46 +00:00
|
|
|
state,
|
2022-08-29 18:09:28 +00:00
|
|
|
});
|
|
|
|
|
2022-09-11 09:31:09 +00:00
|
|
|
const cookies = new Cookies(req, res, { keys: COOKIE_KEYS, secure: inProd });
|
2022-08-29 18:34:46 +00:00
|
|
|
|
|
|
|
cookies.set('validation', stateToken, {
|
2022-09-11 09:31:09 +00:00
|
|
|
secure: inProd,
|
2022-08-29 18:34:46 +00:00
|
|
|
signed: true,
|
|
|
|
});
|
2022-08-29 18:09:28 +00:00
|
|
|
|
|
|
|
res.redirect(`${OAUTH_URL}/authorize?${params.toString()}`);
|
|
|
|
}
|