-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(prisma-transaction): remove Promise.all in Prisma transaction (#21)
* fix(prisma-transaction): remove Promise.all in Prisma transaction * fix(forward-error): forward 500 error and improve health check
- Loading branch information
Showing
14 changed files
with
515 additions
and
371 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,80 @@ | ||
import { ethers } from "ethers"; | ||
import { Request, Response } from "express"; | ||
import { SiweErrorType, SiweMessage } from "siwe"; | ||
import { createOrUpdateUser } from "../services/prisma"; | ||
import { ethers } from 'ethers' | ||
import { Request, Response } from 'express' | ||
import { SiweErrorType, SiweMessage } from 'siwe' | ||
import { createOrUpdateUser } from '../services/prisma' | ||
|
||
const provider = new ethers.InfuraProvider( | ||
"mainnet", | ||
process.env.INFURA_API_KEY | ||
); | ||
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 { | ||
if (!req.body.message) { | ||
res | ||
.status(422) | ||
.json({ message: "Expected prepareMessage object as body." }); | ||
return; | ||
return res.status(422).json({ message: 'Expected prepareMessage object as body.' }) | ||
} | ||
|
||
const message = new SiweMessage(req.body.message); | ||
const message = new SiweMessage(req.body.message) | ||
const fields = await message.verify( | ||
{ | ||
signature: req.body.signature, | ||
nonce: req.session.nonce, | ||
nonce: req.session.nonce | ||
}, | ||
{ | ||
provider, | ||
provider | ||
} | ||
); | ||
) | ||
|
||
req.session.siwe = fields.data; | ||
req.session.siwe = fields.data | ||
if (!fields.data.expirationTime) { | ||
return res.status(422).json({ | ||
message: "Expected expirationTime to be set.", | ||
}); | ||
message: 'Expected expirationTime to be set.' | ||
}) | ||
} | ||
req.session.cookie.expires = new Date(fields.data.expirationTime); | ||
req.session.cookie.expires = new Date(fields.data.expirationTime) | ||
|
||
const { accessToken, refreshToken } = await createOrUpdateUser(fields.data); | ||
const { accessToken, refreshToken } = await createOrUpdateUser(fields.data) | ||
|
||
return req.session.save(() => { | ||
return res.status(200).json({ | ||
accessToken: accessToken, | ||
refreshToken: refreshToken, | ||
}); | ||
}); | ||
refreshToken: refreshToken | ||
}) | ||
}) | ||
} catch (e: any) { | ||
console.error(e); | ||
req.session.siwe = undefined; | ||
req.session.nonce = undefined; | ||
console.error(e) | ||
req.session.siwe = undefined | ||
req.session.nonce = undefined | ||
try { | ||
switch (e) { | ||
case SiweErrorType.EXPIRED_MESSAGE: { | ||
req.session.save(() => res.status(440).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(440).json({ message: e.message })) | ||
break | ||
} | ||
case SiweErrorType.INVALID_SIGNATURE: { | ||
req.session.save(() => res.status(422).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(422).json({ message: e.message })) | ||
break | ||
} | ||
case SiweErrorType.INVALID_ADDRESS: { | ||
req.session.save(() => res.status(422).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(422).json({ message: e.message })) | ||
break | ||
} | ||
case SiweErrorType.NONCE_MISMATCH: { | ||
req.session.save(() => res.status(400).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(400).json({ message: e.message })) | ||
break | ||
} | ||
case SiweErrorType.DOMAIN_MISMATCH: { | ||
req.session.save(() => res.status(400).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(400).json({ message: e.message })) | ||
break | ||
} | ||
default: { | ||
req.session.save(() => res.status(500).json({ message: e.message })); | ||
break; | ||
req.session.save(() => res.status(500).json({ message: e.message ?? `${e}` })) | ||
break | ||
} | ||
} | ||
} catch (sessionError) { | ||
console.error(`Failed to save session, ${JSON.stringify(sessionError)}`); | ||
console.error(`Failed to save session, ${JSON.stringify(sessionError)}`) | ||
} | ||
|
||
return; | ||
return | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.