try to login user

This commit is contained in:
einhornimmond 2021-06-24 18:29:30 +02:00 committed by Ulf Gebhardt
parent c2ecd0d641
commit 8e96aa02df
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
4 changed files with 28 additions and 8 deletions

View File

@ -16,12 +16,14 @@
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^15.5.1",
"jsonwebtoken": "^8.5.1",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1",
"typeorm": "^0.2.34"
},
"devDependencies": {
"@types/express": "^4.17.12",
"@types/jsonwebtoken": "^8.5.2",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"eslint": "^7.29.0",

View File

@ -1,5 +1,8 @@
import axios from 'axios'
import { graphql } from 'graphql'
import CONFIG from '../config'
import { User } from '../graphql/models/User'
import { LoginUserInput } from '../graphql/inputs/LoginUserInput'
// eslint-disable-next-line no-unused-vars
// import regeneratorRuntime from 'regenerator-runtime'
@ -51,13 +54,19 @@ interface NetworkInfosResult {
}
}
const loginAPI = {
login: async (email: string, password: string): Promise<any> => {
const payload: any = {
email,
password,
interface LoginResult {
state: string
msg?: string
details?: number
info?: string
user?: User
// eslint-disable-next-line camelcase
session_id?: number
}
return apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', payload)
const loginAPI = {
login: async (login: LoginUserInput): Promise<LoginResult> => {
return (await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', login)).result.data
},
logout: async (sessionId: number): Promise<any> => {
const payload: any = { session_id: sessionId }
@ -172,4 +181,4 @@ const loginAPI = {
},
}
export { loginAPI, NetworkInfosResult }
export { loginAPI, NetworkInfosResult, LoginResult }

View File

@ -1,5 +1,8 @@
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'
@ -15,6 +18,12 @@ export class UserResolver {
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) {

View File

@ -10,7 +10,7 @@ import { GroupResolver } from './graphql/resolvers/GroupResolver'
async function main() {
// const connection = await createConnection()
const schema = await buildSchema({ resolvers: [BookResolver, GroupResolver] })
const schema = await buildSchema({ resolvers: [BookResolver, GroupResolver, UserResolver] })
const server = express()
server.use(