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 {
|
2022-09-11 09:44:43 +00:00
|
|
|
AUTHORIZE_URL,
|
2022-08-29 18:34:46 +00:00
|
|
|
CLIENT_ID,
|
|
|
|
COOKIE_KEYS,
|
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-09-20 14:38:22 +00:00
|
|
|
prompt: 'consent',
|
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
|
|
|
|
2022-09-11 09:44:43 +00:00
|
|
|
res.redirect(`${AUTHORIZE_URL}?${params.toString()}`);
|
2022-08-29 18:09:28 +00:00
|
|
|
}
|