Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: web3modal siwe #26

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ if (!REDIS_PASSWORD) {
throw new ReferenceError('REDIS_PASSWORD missing in environment variables')
}

const isProd = process.env.NODE_ENV === 'production'
const isStage = process.env.NODE_ENV === 'staging'
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 +65,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 +73,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 All @@ -91,7 +91,7 @@ app.use(
store: redisStore,
cookie: {
secure: isDev ? false : true,
sameSite: isProd ? 'strict' : 'none',
sameSite: isStage ? 'none' : 'strict',
maxAge: 144 * 60 * 60 * 1000,
httpOnly: true
}
Expand Down