36 lines
943 B
TypeScript
36 lines
943 B
TypeScript
import { auth0Integration } from "@/integrations"
|
|
import { User } from "@/models"
|
|
import { Op } from "@sequelize/core"
|
|
import BaseService from "@/services/base-service"
|
|
|
|
export class FindFromAuth0TokenService extends BaseService {
|
|
constructor(private token: string) {
|
|
super()
|
|
}
|
|
|
|
async perform(): Promise<User> {
|
|
const { auth0Subject, email } = await auth0Integration.getUserInfo(this.token)
|
|
|
|
const existingUser = await User.withScope(["asCurrentUser"]).findOne({
|
|
where: { auth0Subject },
|
|
})
|
|
|
|
if (existingUser) {
|
|
return existingUser
|
|
}
|
|
|
|
const firstTimeUser = await User.withScope(["asCurrentUser"]).findOne({
|
|
where: { [Op.or]: [{ auth0Subject: email }, { email: email }] },
|
|
})
|
|
|
|
if (firstTimeUser) {
|
|
await firstTimeUser.update({ auth0Subject })
|
|
return firstTimeUser
|
|
}
|
|
|
|
throw new Error("No user found for this token.")
|
|
}
|
|
}
|
|
|
|
export default FindFromAuth0TokenService
|