Created
November 21, 2019 08:58
-
-
Save umutyerebakmaz/f8031d8cc40510e67b06b04975616a28 to your computer and use it in GitHub Desktop.
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
import { Resolver, Query, Ctx, Mutation, Arg, FieldResolver, Root } from 'type-graphql'; | |
import { User, CreateUserInput } from '../entities/User'; | |
import * as crypto from 'crypto-js'; | |
import { File } from '../entities/File'; | |
import { GraphQLUpload } from 'graphql-upload'; | |
import { createWriteStream } from 'fs'; | |
import { Like, getRepository } from 'typeorm'; | |
@Resolver(User) | |
export class UserResolver { | |
@Query(returns => [User]) | |
async allUsers() { | |
return User.find(); | |
} | |
@Query(returns => User) | |
async userById(@Arg('id') id: string) { | |
return User.findOne({ id }) | |
}; | |
@Query(returns => User) | |
async userByUserName(@Arg('userName') userName: string) { | |
return User.findOne({ userName }) | |
}; | |
@Query(returns => User) | |
async userBySlug(@Arg('slug') slug: string) { | |
return User.findOne({ slug }) | |
}; | |
@Query(returns => [User]) | |
async userWithPaginate( | |
@Arg('userName') userName: string, | |
@Arg('offset') offset: string, | |
@Arg('limit') limit: string, | |
) { | |
return User.find({ where: { userName: Like(`%${userName}%`), offset, limit } }); | |
}; | |
// login | |
@Mutation(returns => User, { nullable: true }) | |
async login( | |
@Arg('userName') userName: string, | |
@Arg('password') password: string, | |
@Ctx() context: any, | |
): Promise<User> { | |
const user = await User.findOne({ userName }); | |
const hashedPassword = crypto.SHA256(password).toString(); | |
console.log(user) | |
if (!user) { | |
throw new Error('bad credentials'); // it is inconvenient to show which one is wrong because it is sensitive. | |
} | |
else if (user.password !== hashedPassword) { | |
throw new Error('bad credentials'); // it is inconvenient to show which one is wrong because it is sensitive. | |
} | |
else { | |
context.req.session.userId = user.id; | |
return user; | |
} | |
}; | |
@Mutation(returns => Boolean) | |
async logout( | |
@Ctx() context: any | |
): Promise<Boolean> { | |
const session = context.req.session; | |
const cookie = context.res; | |
if (session && cookie) { | |
session.destroy(); | |
cookie.clearCookie('connect.sid'); | |
return true; | |
} | |
return false; | |
}; | |
// me | |
@Query(returns => User) | |
async me( | |
@Ctx() context: any | |
) { | |
const userId = context.session.userId; | |
if (userId) { | |
const user = await User.findOne({ id: userId }); | |
return user; | |
} else { | |
throw new Error('not logged in'); | |
} | |
}; | |
// register | |
@Mutation(returns => User) | |
async register(@Arg('user') registerInput: CreateUserInput): Promise<CreateUserInput> { | |
const userCheck = await User.findOne({ where: { username: registerInput.userName } }); | |
if (userCheck.userName === registerInput.userName) { | |
throw new Error('username has already been used.'); | |
} | |
else if (userCheck.email === registerInput.email) { | |
throw new Error('email has already been used.') | |
} | |
else if (userCheck.slug === registerInput.slug) { | |
throw new Error('slug has already been used.') | |
} | |
else if (registerInput.password.length < 6) { | |
throw new Error('password must be longer than or equal to 6 characters') | |
} | |
else { | |
const newUser = new User(); | |
const hashedPassword = crypto.SHA256(registerInput.password).toString(); | |
newUser.userName = registerInput.userName; | |
newUser.email = registerInput.email; | |
newUser.slug = registerInput.slug; | |
newUser.password = hashedPassword; | |
newUser.firstName = registerInput.firstName; | |
newUser.lastName = registerInput.lastName; | |
newUser.role = registerInput.role; | |
await newUser.save(); | |
return newUser; | |
} | |
}; | |
// update | |
@Mutation(returns => User) | |
async updateUser(@Arg('id') id: string, @Arg('user') updateUserInput: CreateUserInput): Promise<CreateUserInput> { | |
const userRepository = getRepository(User); | |
const user = await userRepository.findOne({ id }); | |
const hashedPassword = crypto.SHA256(updateUserInput.password).toString(); | |
user.userName = updateUserInput.userName; | |
user.email = updateUserInput.email; | |
user.slug = updateUserInput.slug; | |
user.password = hashedPassword; | |
user.firstName = updateUserInput.firstName; | |
user.lastName = updateUserInput.lastName; | |
user.role = updateUserInput.role; | |
await userRepository.save(user); | |
return user; | |
}; | |
// Field Resolver Örnek | |
@FieldResolver() | |
async name(@Root() parent: User) { | |
return `${parent.firstName + ' ' + parent.lastName}`; | |
}; | |
// ImageUpload | |
@Mutation(returns => Boolean) | |
async imageUpload(@Arg('file', () => GraphQLUpload) file: File) { | |
const { filename, mimetype, createReadStream } = await file; | |
const stream = createReadStream(); | |
stream.pipe(createWriteStream(filename)); | |
return true; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment