added error handling, fixed authenticaton, added toaster
This commit is contained in:
parent
40c35bda13
commit
b6b7cea210
18 changed files with 1327 additions and 68 deletions
|
|
@ -7,6 +7,7 @@ import { BookOneResolver } from './resolvers/bookOneResolver';
|
|||
|
||||
/* Mutation */
|
||||
import { LoginResolver } from './resolvers/auth/login';
|
||||
import { SignUpResolver } from './resolvers/auth/signUp';
|
||||
|
||||
export const Query: QueryResolvers = {
|
||||
booksMany: BooksManyResolver,
|
||||
|
|
@ -14,7 +15,8 @@ export const Query: QueryResolvers = {
|
|||
};
|
||||
|
||||
export const Mutation = {
|
||||
login: LoginResolver
|
||||
login: LoginResolver,
|
||||
signUp: SignUpResolver
|
||||
};
|
||||
|
||||
export const Subscription = {};
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ export async function LoginResolver(
|
|||
{ input }: MutationLoginArgs,
|
||||
{ identity }: IdentityContext
|
||||
): Promise<Session> {
|
||||
const error = 'Invalid username or password';
|
||||
const error = 'Invalid password or email';
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: input.email }
|
||||
});
|
||||
if (!user) throw new Error(error);
|
||||
if (user == null) throw new Error(error);
|
||||
|
||||
const match = await bcrypt.compare(input.password, user.password);
|
||||
if (!match) throw new Error(error);
|
||||
|
|
|
|||
|
|
@ -1,24 +1,24 @@
|
|||
import { IdentityContext } from '@app/server/identity';
|
||||
import { prisma, uuid } from '@app/server/prisma/client';
|
||||
import { RegisterInput } from '@generated/types';
|
||||
import { MutationRegisterArgs } from '@generated/types';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
export async function SignUpResolver(
|
||||
_: any,
|
||||
input: RegisterInput,
|
||||
{ input }: MutationRegisterArgs,
|
||||
{ identity }: IdentityContext
|
||||
): Promise<void> {
|
||||
const existingUsername = await prisma.user.findUnique({
|
||||
where: { username: input.username },
|
||||
select: { id: true }
|
||||
});
|
||||
if (existingUsername != null) throw Error('User with this username already exists.');
|
||||
if (existingUsername != null) throw new Error('User with this username already exists.');
|
||||
|
||||
const existingEmail = await prisma.user.findUnique({
|
||||
where: { email: input.email },
|
||||
select: { id: true }
|
||||
});
|
||||
if (existingEmail != null) throw Error('Email is already in use.');
|
||||
if (existingEmail != null) throw new Error('Email is already in use.');
|
||||
|
||||
const hashedPassword = await bcrypt.hash(input.password, 10);
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ type Query {
|
|||
|
||||
type Mutation {
|
||||
login(input: LoginInput!): Session!
|
||||
register(input: RegisterInput!): Void!
|
||||
signUp(input: RegisterInput!): Void
|
||||
}
|
||||
|
||||
input LoginInput {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,14 @@ const resolvers = {
|
|||
const apollo = new ApolloServer<IdentityContext>({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })]
|
||||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
|
||||
formatError: (formattedError, error) => {
|
||||
process.stdout.write('\n```\n');
|
||||
console.error(`${error}`);
|
||||
process.stdout.write('\n```\n');
|
||||
|
||||
return formattedError;
|
||||
}
|
||||
});
|
||||
|
||||
await apollo.start();
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ model User {
|
|||
username String @unique @db.VarChar(255)
|
||||
password String
|
||||
|
||||
refreshToken String?
|
||||
refreshToken String? @db.VarChar(255)
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue