Skip to content

Instantly share code, notes, and snippets.

@NetLancer
Last active July 20, 2023 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetLancer/cdab1330659cbe209a89f5654f9e7559 to your computer and use it in GitHub Desktop.
Save NetLancer/cdab1330659cbe209a89f5654f9e7559 to your computer and use it in GitHub Desktop.
export const getCurrentUser = async (session) => {
if (!session || typeof session.id !== 'number') {
throw new Error('Invalid session')
}
const qResult = await db.user.findUnique({
where: { id: session.id },
select: { id: true, email: true, roles: true },
})
if (qResult?.roles?.includes(",")) { // roles string includes commas
const arr = []
qResult.roles.split(',').map(r => {
r.length && (arr.push(r.trim())) // treat if accidental trailing comma ..
})
qResult.roles = arr // now we have a neat array of values
}
return qResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment