MCPcopy Create free account
hub / github.com/code100x/cms / withMobileAuth

Function withMobileAuth

src/middleware.ts:31–57  ·  view source on GitHub ↗
(req: RequestWithUser)

Source from the content-addressed store, hash-verified

29};
30
31export const withMobileAuth = async (req: RequestWithUser) => {
32 if (req.headers.get('Auth-Key')) {
33 return NextResponse.next();
34 }
35 const token = req.headers.get('Authorization');
36
37 if (!token) {
38 return NextResponse.json({ message: 'Unauthorized' }, { status: 403 });
39 }
40 const payload = await verifyJWT(token);
41 if (!payload) {
42 return NextResponse.json({ message: 'Unauthorized' }, { status: 403 });
43 }
44 const newHeaders = new Headers(req.headers);
45
46 /**
47 * Add a global object 'g'
48 * it holds the request claims and other keys
49 * easily pass around this key as request context
50 */
51 newHeaders.set('g', JSON.stringify(payload));
52 return NextResponse.next({
53 request: {
54 headers: newHeaders,
55 },
56 });
57};
58
59const withAuth = async (req: NextRequestWithAuth) => {
60 if (process.env.LOCAL_CMS_PROVIDER) return;

Callers 1

middlewareFunction · 0.85

Calls 3

verifyJWTFunction · 0.85
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected