-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from chingu-x/feature/discord-oauth-p2
Feature/discord oauth p2 - functionality
- Loading branch information
Showing
13 changed files
with
165 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default [ | ||
{ | ||
name: "discord", | ||
}, | ||
]; |
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
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,22 +1,78 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Injectable, InternalServerErrorException } from "@nestjs/common"; | ||
import { IAuthProvider } from "../global/interfaces/oauth.interface"; | ||
import { PrismaService } from "../prisma/prisma.service"; | ||
import { DiscordUser } from "../global/types/auth.types"; | ||
import { generatePasswordHash } from "../utils/auth"; | ||
import { AuthService } from "./auth.service"; | ||
|
||
@Injectable() | ||
export class DiscordAuthService implements IAuthProvider { | ||
constructor(private prisma: PrismaService) {} | ||
constructor( | ||
private prisma: PrismaService, | ||
private authService: AuthService, | ||
) {} | ||
|
||
async validateUser(user: DiscordUser) { | ||
const userInDb = await this.prisma.findUserByOAuthId( | ||
"discord", | ||
user.discordId, | ||
); | ||
console.log( | ||
`discord-auth.service.ts (14): userInDb = ${JSON.stringify(userInDb)}`, | ||
); | ||
|
||
if (userInDb) | ||
return { | ||
id: userInDb.userId, | ||
email: user.email, | ||
}; | ||
return this.createUser(user); | ||
} | ||
|
||
createUser() {} | ||
async createUser(user: DiscordUser) { | ||
// generate a random password and not tell them so they can't login, but they will be able to reset password, | ||
// and will be able to login with this in future, | ||
// or maybe the app will prompt user the input the password, exact oauth flow is to be determined | ||
|
||
findUserById() {} | ||
// this should not happen when "email" is in the scope | ||
if (!user.email) | ||
throw new InternalServerErrorException( | ||
"[discord-auth.service]: Cannot get email from discord to create a new Chingu account", | ||
); | ||
|
||
// check if email is in the database, add oauth profile to existing account, otherwise, create a new user account | ||
return this.prisma.user.upsert({ | ||
where: { | ||
email: user.email, | ||
}, | ||
update: { | ||
emailVerified: true, | ||
oAuthProfiles: { | ||
create: { | ||
provider: { | ||
connect: { | ||
name: "discord", | ||
}, | ||
}, | ||
providerUserId: user.discordId, | ||
providerUsername: user.username, | ||
}, | ||
}, | ||
}, | ||
create: { | ||
email: user.email, | ||
password: await generatePasswordHash(), | ||
emailVerified: true, | ||
avatar: `https://cdn.discordapp.com/avatars/${user.discordId}/${user.avatar}.png`, | ||
oAuthProfiles: { | ||
create: { | ||
provider: { | ||
connect: { | ||
name: "discord", | ||
}, | ||
}, | ||
providerUserId: user.discordId, | ||
providerUsername: user.username, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
} |
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,7 +1,8 @@ | ||
import { DiscordUser } from "../types/auth.types"; | ||
|
||
export interface IAuthProvider { | ||
validateUser(user: DiscordUser); | ||
createUser(); | ||
findUserById(); | ||
// TODO: Maybe change it to OAuthUser: DiscordUser | GithubUser etc | ||
// Or change it to a more general type name | ||
validateUser(user: DiscordUser): void; | ||
createUser(user: DiscordUser): void; | ||
} |
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
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