-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3576ec7
commit 63da809
Showing
7 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { Request, Response } from 'express' | ||
import { handleAndReturnErrorResponse } from '../../schema/errors' | ||
import { EthereumAddressSchema } from '../../schema/zod/schemas/base/ethereumAddress' | ||
import { RequestHeadersSchema } from '../../schema/zod/schemas/auth' | ||
import prisma from '../../utils/prismaClient' | ||
|
||
/** | ||
* Function for route /auth/users/:address/check-status | ||
* Returns whether a given address is registered on EE, if they are an EL staker & if we track their rewards | ||
* | ||
* @param req | ||
* @param res | ||
* @returns | ||
*/ | ||
export async function checkUserStatus(req: Request, res: Response) { | ||
const paramCheck = EthereumAddressSchema.safeParse(req.params.address) | ||
if (!paramCheck.success) { | ||
return handleAndReturnErrorResponse(req, res, paramCheck.error) | ||
} | ||
|
||
try { | ||
const { address } = req.params | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { address: address.toLowerCase() }, | ||
include: { staker: true } | ||
}) | ||
|
||
const isRegistered = !!user | ||
const isStaker = !!user?.staker | ||
const isTracked = !!user?.isTracked | ||
|
||
res.send({ isRegistered, isStaker, isTracked }) | ||
} catch (error) { | ||
handleAndReturnErrorResponse(req, res, error) | ||
} | ||
} | ||
|
||
/** | ||
* Function for route /auth/users/:address/register | ||
* Protected route, adds an address to the User table if it doesn't exist | ||
* | ||
* @param req | ||
* @param res | ||
* @returns | ||
*/ | ||
export async function registerUser(req: Request, res: Response) { | ||
const paramCheck = EthereumAddressSchema.safeParse(req.params.address) | ||
if (!paramCheck.success) { | ||
return handleAndReturnErrorResponse(req, res, paramCheck.error) | ||
} | ||
|
||
const headerCheck = RequestHeadersSchema.safeParse(req.headers) | ||
if (!headerCheck.success) { | ||
return handleAndReturnErrorResponse(req, res, headerCheck.error) | ||
} | ||
|
||
try { | ||
const apiToken = headerCheck.data['X-API-Token'] | ||
const authToken = process.env.EE_AUTH_TOKEN | ||
|
||
if (!apiToken || apiToken !== authToken) { | ||
throw new Error('Unauthorized access.') | ||
} | ||
|
||
const { address } = req.params | ||
|
||
const existingUser = await prisma.user.findUnique({ | ||
where: { address: address.toLowerCase() } | ||
}) | ||
|
||
if (!existingUser) { | ||
await prisma.user.create({ | ||
data: { | ||
address: address.toLowerCase(), | ||
isTracked: false | ||
} | ||
}) | ||
} | ||
|
||
res.send({ isNewUser: !existingUser }) | ||
} catch (error) { | ||
handleAndReturnErrorResponse(req, res, error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from 'express' | ||
import routeCache from 'route-cache' | ||
import { checkUserStatus, registerUser } from './authController' | ||
|
||
const router = express.Router() | ||
|
||
// API routes for /auth | ||
|
||
router.get('/users/:address/check-status', routeCache.cacheSeconds(30), checkUserStatus) | ||
router.post('/users/:address/register', routeCache.cacheSeconds(240), registerUser) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import z from '..' | ||
|
||
export const RequestHeadersSchema = z | ||
.object({ | ||
'x-api-token': z.string().optional() | ||
}) | ||
.transform((headers) => { | ||
const token = Object.keys(headers).find((key) => key.toLowerCase() === 'x-api-token') | ||
return token | ||
? { | ||
'X-API-Token': headers[token] | ||
} | ||
: {} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters