User Login finished

This commit is contained in:
s-prechtl 2023-01-18 11:35:38 +01:00
parent 4f936aaa42
commit c52f6018a6
6 changed files with 311 additions and 67 deletions

View file

@ -84,3 +84,30 @@ userRoute.get('/:userId/scores',
} }
} }
) )
userRoute.get('/:name',
param('name')
.isString()
.isLength({min: 3, max: 32})
.matches(USERNAME_VALIDATION_REGEX),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const name: string = req.params.name;
console.log(name)
try {
// get & return data
const userRepo: UserPgPromiseRepository = new UserPgPromiseRepository();
const user = await userRepo.getByName(name);
res.json(user);
} catch (error) {
// handle errors
console.log(error)
res.status(500).json({ errors: [{msg: "Internal server error"}]})
}
})

File diff suppressed because it is too large Load diff

View file

@ -15,14 +15,8 @@
"vue": "^3.2.13" "vue": "^3.2.13"
}, },
"devDependencies": { "devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-plugin-typescript": "~5.0.0", "@vue/cli-plugin-typescript": "~5.0.0",
"@vue/cli-service": "~5.0.0", "@vue/cli-service": "~5.0.0",
"@vue/eslint-config-typescript": "^9.1.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"typescript": "~4.5.5" "typescript": "~4.5.5"
} }
} }

View file

@ -7,8 +7,10 @@
<UserScores :userScores="userScores" ></UserScores> <UserScores :userScores="userScores" ></UserScores>
</div> </div>
<div class="row"> <div class="row">
<Game class="col"> <Game v-if="user" class="col">
</Game> </Game>
<Login v-else @userChange="(user) => this.user = user">
</Login>
</div> </div>
<div class="row"> <div class="row">
<div class="col"> <div class="col">
@ -30,10 +32,13 @@ import Header from './components/Header.vue';
import "bootstrap/dist/css/bootstrap.min.css"; import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js"; import "bootstrap/dist/js/bootstrap.min.js";
import Login from "@/components/Login.vue";
import {User} from "@/model/User";
export default defineComponent({ export default defineComponent({
name: 'App', name: 'App',
components: { components: {
Login,
UserScores, UserScores,
Leaderboard, Leaderboard,
Game, Game,
@ -45,6 +50,7 @@ export default defineComponent({
higscoreLeaderboard: [], higscoreLeaderboard: [],
totalPlaytimeLeaderboard: [], totalPlaytimeLeaderboard: [],
userId: 1, userId: 1,
user: null,
} }
}, },
methods: { methods: {

View file

@ -0,0 +1,45 @@
<template>
<h2>Enter a username</h2>
<div class="form-floating mb-3">
<input type="email" class="form-control" id="floatingInput" placeholder="example name" v-model="username">
<label for="floatingInput">Username</label>
</div>
<button type="button" class="btn btn-primary" @click="setUser()">Confirm</button>
</template>
<script>
import {User} from "@/model/User";
export default {
name: "Login",
data() {
return {
username: '',
user: null,
}
},
emits: ['userChange'],
methods: {
async setUser() {
let user = await User.getByName(this.username);
if (user.errors) {
user = await User.create(this.username);
}
if (user.errors) {
console.error("Something when wrong when logging in, please contact admin!")
return;
}
if (user) {
this.user = user;
this.$emit('userChange', this.user);
}
},
}
}
</script>
<style scoped>
</style>

View file

@ -1,4 +1,32 @@
export interface User { export class User {
id?: number, id?: number;
name: string, name?: string;
constructor(id: number, name: string) {
this.name = name;
}
static async getByName(name: string): Promise<User> {
let res: Response = await fetch('http://localhost:3000/user/' + name, {method: 'GET'});
return await res.json();
}
static async create(name: string): Promise<User> {
let body = {
name: name
};
let header = {
Accept: "application/json",
"Content-Type": "application/json",
};
let res: Response = await fetch('http://localhost:3000/user/register', {
method: 'POST',
body: JSON.stringify(body),
headers: header,
});
return await res.json();
}
} }