Frontend login finished

This commit is contained in:
viktorstrate
2019-07-07 15:54:39 +02:00
parent d3f9ed8116
commit 1604827f35
18 changed files with 441 additions and 21738 deletions

7866
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -25,7 +25,8 @@
"jsonwebtoken": "^8.5.1",
"neo4j-driver": "^1.7.3",
"neo4j-graphql-js": "^2.6.3",
"node-fetch": "^2.3.0"
"node-fetch": "^2.3.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",

View File

@@ -6,6 +6,7 @@ import { v1 as neo4j } from "neo4j-driver";
import { makeAugmentedSchema } from "neo4j-graphql-js";
import dotenv from "dotenv";
import jwt from 'jsonwebtoken'
import uuid from 'uuid'
// set environment variables from ../.env
dotenv.config();
@@ -27,13 +28,62 @@ const schema = makeAugmentedSchema({
auth: {
isAuthenticated: true,
hasRole: true
}
},
mutation: false
},
resolvers: {
Mutation: {
authorizeUser(root, args, context, info) {
const token = jwt.sign({name: args.name}, process.env.JWT_SECRET)
return token
async authorizeUser(root, args, ctx, info) {
let {username, password} = args
let session = ctx.driver.session()
let result = await session.run("MATCH (usr:User {username: {username}, password: {password} }) RETURN usr.id", {username, password})
if (result.records.length == 0) {
return {
success: false,
status: "Username or password was invalid",
token: null
}
}
const record = result.records[0]
const userId = record.get('usr.id')
const token = jwt.sign({id: userId}, process.env.JWT_SECRET)
return {
success: true,
status: "Authorized",
token
}
},
async registerUser(root, args, ctx, info) {
let {username, password} = args
let session = ctx.driver.session()
let result = await session.run("MATCH (usr:User {username: {username} }) RETURN usr", {username})
if (result.records.length > 0) {
return {
success: false,
status: "Username is already taken",
token: null
}
}
await session.run("CREATE (n:User { username: {username}, password: {password}, id: {id} }) return n", {username, password, id: uuid()})
session.close()
return {
success: true,
status: "User created",
token: "yay"
}
}
}
}

View File

@@ -3,13 +3,35 @@ enum Role {
User
}
type Todo @isAuthenticated {
type User @isAuthenticated {
id: ID!
username: String!
albums: [Album] @relation(name: "OWNS", direction: "OUT")
}
type Album @isAuthenticated {
id: ID!
title: String
photos: [Photo] @relation(name: "CONTAINS", direction: "OUT")
owner: User! @ relation(name: "OWNS", direction: "IN")
}
type Photo @isAuthenticated {
id: ID!
title: String
path: String!
album: Album! @relation(name: "CONTAINS", direction: "IN")
}
type AuthorizeResult {
success: Boolean!
status: String
token: String
}
type Mutation {
authorizeUser(name: String!): String
authorizeUser(username: String!, password: String!): AuthorizeResult!
registerUser(username: String!, password: String!): AuthorizeResult!
}
# type User {