-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve sign in error handling
- Loading branch information
1 parent
06707bd
commit 2fd95d4
Showing
5 changed files
with
105 additions
and
59 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 |
---|---|---|
@@ -1,56 +1,76 @@ | ||
import { generateToken } from "@/auth"; | ||
import type { Context } from "@/context"; | ||
import { compare } from "bcrypt"; | ||
import { GraphQLNonNull, GraphQLString } from "graphql"; | ||
import { | ||
GraphQLEnumType, | ||
GraphQLList, | ||
GraphQLNonNull, | ||
GraphQLString, | ||
} from "graphql"; | ||
import { mutationWithClientMutationId } from "graphql-relay"; | ||
import { GraphQLNonEmptyString } from "graphql-scalars"; | ||
import { type User, UserModel } from "../UserModel"; | ||
import { UserType } from "../UserType"; | ||
|
||
type LoginInput = { | ||
username?: string; | ||
email?: string; | ||
password: string; | ||
username?: string; | ||
email?: string; | ||
password: string; | ||
}; | ||
|
||
type LoginErrors = "INVALID_CREDENTIALS" | "INVALID_PASSWORD"; | ||
|
||
type LoginOutput = { | ||
user: User; | ||
token: string; | ||
user: User | null; | ||
token: string | null; | ||
errors: LoginErrors[] | null; | ||
}; | ||
|
||
export const Login = mutationWithClientMutationId< | ||
LoginInput, | ||
Promise<LoginOutput>, | ||
Context | ||
LoginInput, | ||
Promise<LoginOutput>, | ||
Context | ||
>({ | ||
name: "LoginMutation", | ||
inputFields: { | ||
password: { type: GraphQLNonEmptyString }, | ||
email: { type: GraphQLString }, | ||
username: { type: GraphQLString }, | ||
}, | ||
outputFields: { | ||
token: { type: new GraphQLNonNull(GraphQLString) }, | ||
user: { type: UserType }, | ||
}, | ||
mutateAndGetPayload: async ({ email, username, password }) => { | ||
const user = await UserModel.findOne({ | ||
$or: [ | ||
{ email: email?.toLowerCase()?.trim() }, | ||
{ username: username?.trim() }, | ||
], | ||
}).select("+password"); | ||
|
||
if (!user) { | ||
throw new Error("Invalid email or username"); | ||
} | ||
|
||
if (!(await compare(password, user.password))) { | ||
throw new Error("Invalid password"); | ||
} | ||
|
||
const token = generateToken(user); | ||
|
||
return { token, user }; | ||
}, | ||
name: "LoginMutation", | ||
inputFields: { | ||
password: { type: GraphQLNonEmptyString }, | ||
email: { type: GraphQLString }, | ||
username: { type: GraphQLString }, | ||
}, | ||
outputFields: { | ||
token: { type: GraphQLString }, | ||
user: { type: UserType }, | ||
errors: { | ||
type: new GraphQLList( | ||
new GraphQLNonNull( | ||
new GraphQLEnumType({ | ||
name: "LoginMutationErrors", | ||
values: { | ||
INVALID_CREDENTIALS: { value: "INVALID_CREDENTIALS" }, | ||
INVALID_PASSWORD: { value: "INVALID_PASSWORD" }, | ||
}, | ||
}), | ||
), | ||
), | ||
}, | ||
}, | ||
mutateAndGetPayload: async ({ email, username, password }) => { | ||
const cleanEmail = email?.toLowerCase()?.trim(); | ||
const cleanUsername = username?.trim(); | ||
|
||
const user = await UserModel.findOne({ | ||
$or: [{ email: cleanEmail }, { username: cleanUsername }], | ||
}).select("+password"); | ||
|
||
if (!user) { | ||
return { errors: ["INVALID_CREDENTIALS"], token: null, user: null }; | ||
} | ||
if (!(await compare(password, user.password))) { | ||
return { errors: ["INVALID_PASSWORD"], token: null, user: null }; | ||
} | ||
|
||
const token = generateToken(user); | ||
|
||
return { token, user, errors: null }; | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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