Signup from game
This commit is contained in:
parent
60c2da0429
commit
33b47dcb67
6 changed files with 88 additions and 18 deletions
25
game/src/api.ts
Normal file
25
game/src/api.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
const baseApiPath = "http://localhost:3000"
|
||||
|
||||
export default {
|
||||
async post(path: string, data: any) {
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(`${baseApiPath}/${path}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async signup(name: string) {
|
||||
return this.post("signup", { name })
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,14 @@
|
|||
declare interface Window { myStuff: any }
|
||||
import API from './api'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
myStuff: {
|
||||
name?: String | null,
|
||||
token?: String | null,
|
||||
password?: String | null,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(() => {
|
||||
const elements = {
|
||||
|
|
@ -60,7 +70,7 @@ declare interface Window { myStuff: any }
|
|||
elements.key!.innerText = response.token
|
||||
}
|
||||
|
||||
function signup() {
|
||||
async function signup() {
|
||||
const nickname = prompt("Your nickname:")
|
||||
|
||||
if (!nickname) {
|
||||
|
|
@ -68,29 +78,18 @@ declare interface Window { myStuff: any }
|
|||
return
|
||||
}
|
||||
|
||||
// TODO: Fetch data from server
|
||||
|
||||
//mock bad request
|
||||
if (nickname == 'Hgs') {
|
||||
alert("The name is occupied by someone else, try again with another nickname")
|
||||
return
|
||||
}
|
||||
|
||||
const response = {
|
||||
token: Math.random().toString() + '.abc',
|
||||
name: nickname!
|
||||
}
|
||||
const response = await API.signup(nickname)
|
||||
|
||||
localStorage.setItem('token', response.token)
|
||||
localStorage.setItem('name', response.name)
|
||||
|
||||
window.myStuff.token = response.token
|
||||
window.myStuff.name = response.name
|
||||
window.myStuff.password = response.password
|
||||
|
||||
elements.bar.logged!.style.display = ''
|
||||
elements.bar.loggedout!.style.display = 'none'
|
||||
elements.name!.innerText = response.name
|
||||
elements.key!.innerText = response.token
|
||||
elements.key!.innerText = response.password
|
||||
}
|
||||
|
||||
function logout() {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2016",
|
||||
"target": "es5",
|
||||
"module": "es6",
|
||||
"strict": true,
|
||||
"lib": [
|
||||
"es2015",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": false,
|
||||
"noEmit": true,
|
||||
"allowJs": true,
|
||||
|
|
@ -15,7 +19,9 @@
|
|||
"sourceMap": true,
|
||||
"baseUrl": "./src",
|
||||
"paths": {
|
||||
"~/*": ["./*"]
|
||||
"~/*": [
|
||||
"./*"
|
||||
]
|
||||
},
|
||||
"typeRoots": [
|
||||
"node_modules/@types",
|
||||
|
|
|
|||
35
server/package-lock.json
generated
35
server/package-lock.json
generated
|
|
@ -13,6 +13,7 @@
|
|||
"@babel/core": "^7.15.5",
|
||||
"@babel/node": "^7.15.4",
|
||||
"@babel/preset-env": "^7.15.6",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
|
|
@ -2258,6 +2259,18 @@
|
|||
"semver": "bin/semver.js"
|
||||
}
|
||||
},
|
||||
"node_modules/cors": {
|
||||
"version": "2.8.5",
|
||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||
"dependencies": {
|
||||
"object-assign": "^4",
|
||||
"vary": "^1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/crypto-random-string": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
||||
|
|
@ -3681,6 +3694,14 @@
|
|||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/object-inspect": {
|
||||
"version": "1.11.0",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.11.0.tgz",
|
||||
|
|
@ -6282,6 +6303,15 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"cors": {
|
||||
"version": "2.8.5",
|
||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||
"requires": {
|
||||
"object-assign": "^4",
|
||||
"vary": "^1"
|
||||
}
|
||||
},
|
||||
"crypto-random-string": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
||||
|
|
@ -7342,6 +7372,11 @@
|
|||
"integrity": "sha512-9UZCFRHQdNrfTpGg8+1INIg93B6zE0aXMVFkw1WFwvO4SlZywU6aLg5Of0Ap/PgcbSw4LNxvMWXMeugwMCX0AA==",
|
||||
"dev": true
|
||||
},
|
||||
"object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
|
||||
},
|
||||
"object-inspect": {
|
||||
"version": "1.11.0",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.11.0.tgz",
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
"@babel/core": "^7.15.5",
|
||||
"@babel/node": "^7.15.4",
|
||||
"@babel/preset-env": "^7.15.6",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import 'dotenv/config'
|
|||
import express from 'express'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import morgan from 'morgan'
|
||||
import cors from 'cors'
|
||||
|
||||
import auth from './Auth.js'
|
||||
import db from './Database.js'
|
||||
|
|
@ -28,6 +29,7 @@ const protect = (req, res, next) => {
|
|||
const app = express()
|
||||
app.use(morgan('combined'))
|
||||
app.use(express.json())
|
||||
app.use(cors())
|
||||
|
||||
app.get("/", (_, res) => {
|
||||
res.json({ message: "Hello! API here" })
|
||||
|
|
@ -39,6 +41,8 @@ app.get("/secured", protect, (req, res) => {
|
|||
})
|
||||
|
||||
app.post("/signup", async (req, res) => {
|
||||
console.log("No co jest??", req.body)
|
||||
|
||||
if (!req.body.name)
|
||||
return res.status(400).json({
|
||||
error: "missing argument name"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue