티스토리 수익 글 보기
Was this helpful?
import { Request, Response } from 'express';
import * as jose from 'jose';
import { getUserInfo } from '../services/user-info-service';
import { getFeatureFlags } from '../services/feature-flags-service';
const GITBOOK_VISITOR_SIGNING_KEY = process.env.GITBOOK_VISITOR_SIGNING_KEY!;
const GITBOOK_DOCS_URL = 'https://mycompany.gitbook.io/myspace';
export async function handleAppLoginRequest(req: Request, res: Response) {
// Your business logic for handling the login request
// For example, checking credentials and authenticating the user
//
// e.g.:
// const loggedInUser = await authenticateUser(req.body.username, req.body.password);
// Generate a signed JWT
const gitbookVisitorJWT = await new jose.SignJWT({})
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h') // Arbitrary 2-hour expiration
.sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
// Redirect the user to GitBook with the JWT token in the URL
const redirectURL = `${GITBOOK_DOCS_URL}/?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);
}const gitbookVisitorJWT = await new jose.SignJWT({})
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h') // Arbitrary 2-hour expiration
.sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
// Redirect to the original GitBook docs URL with the JWT included as jwt_token query parameter
// If a location is provided, the user will be redirected back to their original destination
const redirectURL = `${GITBOOK_DOCS_URL}/${req.query.location || ''}?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);const CUSTOMER_A = {
jwtSigningKey: 'aaa-aaa-aaa-aaa',
url: 'https://mycompany.gitbook.io/customer-a'
};
const CUSTOMER_B = {
jwtSigningKey: 'bbb-bbb-bbb-bbb',
url: 'https://mycompany.gitbook.io/customer-b'
};const customerInfo = req.query.site === 'customer-a' ? CUSTOMER_A : CUSTOMER_B;
const gitbookVisitorJWT = await new jose.SignJWT({})
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h') // Arbitrary 2-hour expiration
.sign(new TextEncoder().encode(customerInfo.jwtSigningKey));
// Redirect to the original GitBook docs URL with the JWT included as jwt_token query parameter
// If a location is provided, the user will be redirected back to their original destination
const redirectURL = `${customerInfo.url}/${req.query.location || ''}?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);import { Request, Response } from 'express';
import * as jose from 'jose';
import { getUserInfo } from '../services/user-info-service';
import { getFeatureFlags } from '../services/feature-flags-service';
const GITBOOK_VISITOR_SIGNING_KEY = process.env.GITBOOK_VISITOR_SIGNING_KEY!;
const GITBOOK_DOCS_URL = 'https://mycompany.gitbook.io/myspace';
export async function handleAppLoginRequest(req: Request, res: Response) {
// Your business logic for handling the login request
// For example, checking credentials and authenticating the user
//
// e.g.:
// const loggedInUser = await authenticateUser(req.body.username, req.body.password);
// For the purpose of this example, assume a logged-in user object
const loggedInUser = { id: '12345' }; // Replace with actual authentication logic
// Retrieve user information to pass to GitBook
const userInfo = await getUserInfo(loggedInUser.id);
// Generate a signed JWT and include the user attributes as claims
const gitbookVisitorClaims = {
firstName: userInfo.firstName,
lastName: userInfo.lastName,
isBetaUser: userInfo.isBetaUser,
products: userInfo.products.map((product) => product.name),
featureFlags: await getFeatureFlags({ userId: loggedInUser.id })
};
const gitbookVisitorJWT = await new jose.SignJWT(gitbookVisitorClaims)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('2h') // Arbitrary 2-hour expiration
.sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
// Redirect the user to GitBook with the JWT token in the URL
const redirectURL = `${GITBOOK_DOCS_URL}/?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);
}