started login functionality & graphql schema

This commit is contained in:
Ulf Gebhardt 2021-06-29 12:24:45 +02:00
parent 50a762073a
commit fd54febd24
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
4 changed files with 92 additions and 55 deletions

View File

@ -5,8 +5,10 @@ dotenv.config()
const server = {
PORT: process.env.PORT || 4000,
JWT_SECRET: process.env.JWT_SECRET || 'secret123',
JWT_EXPIRES_IN: process.env.JWT_EXPIRES_IN || '10m',
GRAPHIQL: process.env.GRAPHIQL === 'true' || false,
// LOGIN_API_URL: process.env.LOGIN_API_URL || 'http://localhost/login_api/',
LOGIN_API_URL: process.env.LOGIN_API_URL || 'http://localhost/login_api/',
// COMMUNITY_API_URL: process.env.COMMUNITY_API_URL || 'http://localhost/api/',
}

View File

@ -0,0 +1,84 @@
import jwt from 'jsonwebtoken'
import axios from 'axios'
import { Resolver, Query, /* Mutation, */ Arg } from 'type-graphql'
import CONFIG from '../../config'
// import { User } from '../models/User'
// import { LoginUserInput } from '../inputs/LoginUserInput'
// import { loginAPI, LoginResult } from '../../apis/loginAPI'
// import { CreateBookInput } from '../inputs/CreateBookInput'
// import { UpdateBookInput } from '../inputs/UpdateBookInput'
const apiPost = async (url: string, payload: any): Promise<any> => {
try {
const result = await axios.post(url, payload)
if (result.status !== 200) {
throw new Error('HTTP Status Error ' + result.status)
}
if (result.data.state === 'warning') {
return { success: true, result: result.data.errors }
}
if (result.data.state !== 'success') {
throw new Error(result.data.msg)
}
return { success: true, result }
} catch (error) {
return { success: false, result: error }
}
}
@Resolver()
export class UserResolver {
/* @Query(() => [User])
users(): Promise<User[]> {
return User.find()
} */
/* @Query(() => User)
user(@Arg('id') id: string): Promise<User | undefined> {
return User.findOne({ where: { id } })
} */
@Query(() => String)
async login(@Arg('email') email: string, @Arg('password') password: string): Promise<string> {
email = email.trim().toLowerCase()
const result = await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', { email, password })
console.log(result)
// if there is no user, throw an authentication error
if (!result.success) {
throw new Error(result.result)
}
// create and return the json web token
return jwt.sign({ result }, CONFIG.JWT_SECRET, { expiresIn: CONFIG.JWT_EXPIRES_IN })
// return (await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', login)).result.data
// const loginResult: LoginResult = await loginAPI.login(data)
// return loginResult.user ? loginResult.user : new User()
}
/*
@Mutation(() => User)
async createBook(@Arg('data') data: CreateBookInput) {
const book = User.create(data)
await book.save()
return book
}
@Mutation(() => Book)
async updateBook(@Arg('id') id: string, @Arg('data') data: UpdateBookInput) {
const book = await Book.findOne({ where: { id } })
if (!book) throw new Error('Book not found!')
Object.assign(book, data)
await book.save()
return book
}
*/
/* @Mutation(() => Boolean)
async deleteUser(@Arg('id') id: string): Promise<boolean> {
const user = await User.findOne({ where: { id } })
if (!user) throw new Error('User not found!')
await user.remove()
return true
} */
}

View File

@ -1,51 +0,0 @@
import { Resolver, Query, Mutation, Arg } from 'type-graphql'
import { User } from '../models/User'
import jwt from 'jsonwebtoken'
import { LoginUserInput } from '../inputs/LoginUserInput'
import { loginAPI, LoginResult } from '../../apis/loginAPI'
// import { CreateBookInput } from '../inputs/CreateBookInput'
// import { UpdateBookInput } from '../inputs/UpdateBookInput'
@Resolver()
export class UserResolver {
@Query(() => [User])
users(): Promise<User[]> {
return User.find()
}
@Query(() => User)
user(@Arg('id') id: string): Promise<User | undefined> {
return User.findOne({ where: { id } })
}
@Mutation(() => User)
async login(@Arg('data') data: LoginUserInput): Promise<User> {
const loginResult: LoginResult = await loginAPI.login(data)
return loginResult.user ? loginResult.user : new User()
}
/*
@Mutation(() => User)
async createBook(@Arg('data') data: CreateBookInput) {
const book = User.create(data)
await book.save()
return book
}
@Mutation(() => Book)
async updateBook(@Arg('id') id: string, @Arg('data') data: UpdateBookInput) {
const book = await Book.findOne({ where: { id } })
if (!book) throw new Error('Book not found!')
Object.assign(book, data)
await book.save()
return book
}
*/
@Mutation(() => Boolean)
async deleteUser(@Arg('id') id: string): Promise<boolean> {
const user = await User.findOne({ where: { id } })
if (!user) throw new Error('User not found!')
await user.remove()
return true
}
}

View File

@ -6,15 +6,17 @@ import { buildSchema } from 'type-graphql'
import CONFIG from './config'
// TODO move to extern
import { BookResolver } from './graphql/resolvers/BookResolver'
// import { UserResolver } from './graphql/resolvers/UserResolver'
// import { BookResolver } from './graphql/resolvers/BookResolver'
import { UserResolver } from './graphql/resolvers/UserResolver'
// import { GroupResolver } from './graphql/resolvers/GroupResolver'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
async function main() {
// const connection = await createConnection()
const schema = await buildSchema({ resolvers: [BookResolver /*, GroupResolver, UserResolver */] })
const schema = await buildSchema({
resolvers: [/* BookResolver , GroupResolver, */ UserResolver],
})
const server = express()
const validationRules: [] = [
/**