Skip to content

Commit

Permalink
fix: web3modal siwe (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsserban authored Jul 2, 2024
1 parent 1b09344 commit 68892e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/handlers/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { Request, Response } from 'express'
import { SiweErrorType, SiweMessage } from 'siwe'
import { createOrUpdateUser } from '../services/prisma'

const provider = new ethers.JsonRpcProvider(
`https://rpc.walletconnect.com/v1?chainId=eip155:1&projectId=${process.env.WALLETCONNECT_PROJECT_ID}`
)
const provider = new ethers.JsonRpcProvider(`https://rpc.walletconnect.com/v1?chainId=eip155:1&projectId=${process.env.WALLETCONNECT_PROJECT_ID}`)

export const verifyAndSignIn = async (req: Request, res: Response) => {
try {
Expand All @@ -25,12 +23,14 @@ export const verifyAndSignIn = async (req: Request, res: Response) => {
)

req.session.siwe = fields.data
if (!fields.data.expirationTime) {
return res.status(422).json({
message: 'Expected expirationTime to be set.'
})

const expirationTime = fields.data.expirationTime
if (expirationTime) {
req.session.cookie.expires = new Date(expirationTime)
} else {
// 7 days from now
req.session.cookie.expires = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
}
req.session.cookie.expires = new Date(fields.data.expirationTime)

const { accessToken, refreshToken } = await createOrUpdateUser(fields.data)

Expand Down
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ if (!REDIS_PASSWORD) {
throw new ReferenceError('REDIS_PASSWORD missing in environment variables')
}

const isProd = process.env.NODE_ENV === 'production'
const isDev = process.env.NODE_ENV === 'development'

const prismaClient = new PrismaClient()

// Initialize redis client
const redisClient = new Redis({
host: REDIS_HOST ?? 'redis',
port: REDIS_PORT ? parseInt(REDIS_PORT, 10) : 6379,
password: REDIS_PASSWORD
password: REDIS_PASSWORD,
tls: {
rejectUnauthorized: isProd ? true : false
}
})

// Initialize connect-redis store for express-session
Expand All @@ -58,9 +64,6 @@ app.disable('x-powered-by')
app.use(express.json())
app.set('trust proxy', 1)

const isProd = process.env.NODE_ENV === 'production'
const isDev = process.env.NODE_ENV === 'development'

const allowedOrigins = isProd
? ['https://cloud.walletconnect.com']
: ['http://localhost', 'https://wc-cloud-staging.vercel.app', /\.?-walletconnect1\.vercel\.app$/]
Expand All @@ -69,11 +72,7 @@ const corsOptions: CorsOptions = {
credentials: true,
methods: ['OPTIONS', 'GET', 'POST'],
origin: (origin, callback) => {
if (
!origin ||
isDev ||
allowedOrigins.some((allowedOrigin) => new RegExp(allowedOrigin).test(origin))
) {
if (!origin || isDev || allowedOrigins.some((allowedOrigin) => new RegExp(allowedOrigin).test(origin))) {
callback(null, true)
} else {
callback(new Error(`Origin ${origin} is not allowed by CORS`))
Expand Down

0 comments on commit 68892e4

Please sign in to comment.