Skip to content

Commit

Permalink
logout, change profile information, change password, unique usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
scammo committed Jan 4, 2024
1 parent dbffe67 commit 6f9cba8
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 11 deletions.
48 changes: 48 additions & 0 deletions backend/app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Track;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
public function me(){
if (!Auth::check()){
abort(403);
}
return Auth::user();
}

public function update(User $user, Request $request){
if (!Auth::check()){
abort(403);
}
if(Auth::user()->username != $user->username){
abort(403);
}
$user->username = $request->username;
$user->email = $request->email;
$user->save();
return $user;
}
public function updatePassword(User $user, Request $request){
if (!Auth::check()){
abort(403);
}
if(Auth::user()->username != $user->username){
abort(403);
}
$user->makeVisible(['password']);
if (Hash::check($request->oldPassword, $user->password)) {
$user->password = Hash::make($request->newPassword);
$user->save();
return $user;
} else {
abort(406);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('username');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
Expand Down
6 changes: 5 additions & 1 deletion backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
Route::get('track/proposals', [\App\Http\Controllers\TrackController::class, 'proposals']);
Route::post('track/users', [\App\Http\Controllers\TrackController::class, 'addUserToTrack']);


Route::post('proposals', [\App\Http\Controllers\ProposalController::class, 'create']);
Route::get('proposal/{proposal}', [\App\Http\Controllers\ProposalController::class, 'show']);


Route::get('user', [\App\Http\Controllers\UserController::class, 'me']);
Route::post('user/{user}', [\App\Http\Controllers\UserController::class, 'update']);
Route::post('user/{user}/password', [\App\Http\Controllers\UserController::class, 'updatePassword']);
5 changes: 3 additions & 2 deletions frontend/src/components/Header.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup>
import { useRoute, useRouter } from "vue-router";
import { setLogout } from '../lib/api'
import lf from "localforage";
import Menubar from "primevue/menubar";
import Button from "primevue/button";
Expand All @@ -8,8 +9,8 @@ const router = useRouter();
const route = useRoute();
const logout = async () => {
//TODO: Build Logout
await await lf.clear();
await lf.clear();
setLogout()
router.push({
name: "login",
});
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const isLoggedIn = () => {
return !!user.value;
}

const setLogin = (paramUser, token) => {
const setLogin = (paramUser, token = null) => {
if (!token) {
const localStorageAuth = JSON.parse(localStorage.getItem("auth"))
token = localStorageAuth.token
}
client.defaults.headers.common['Authorization'] = `Bearer ${token}`;
user.value = paramUser;

Expand All @@ -25,6 +29,10 @@ const setLogin = (paramUser, token) => {
})
);
}
const setLogout = (paramUser, token = null) => {
localStorage.removeItem("auth")
user.value = null
}

const language = ref('en')
const browserLanguage = navigator.language || navigator.userLanguage;
Expand Down Expand Up @@ -69,4 +77,4 @@ const getProposalById = async (id) => {
});
};

export { createProposal, getProposalById, client, user, isLoggedIn, setLogin, i18n };
export { createProposal, getProposalById, client, user, isLoggedIn, setLogin, setLogout, i18n };
97 changes: 92 additions & 5 deletions frontend/src/views/orga/Profile.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,100 @@
<script setup>
import { generateKeypair } from "../../lib/crypto"
import { ref } from "vue";
import { client, user, setLogin } from "../../lib/api";
import Button from "primevue/button";
const onGenerateKeypair = async () => {
console.log(await generateKeypair())
import InputText from "primevue/inputtext";
const me = ref([])
const loading = ref(false);
const email = ref("")
const username = ref("")
const oldPassword = ref("")
const newPassword = ref("")
const loadUser = async () => {
try {
const response = await client.get('user')
me.value = response.data
email.value = me.value.email
username.value = me.value.username
} catch (error) {
console.error(error)
}
}
const onSubmitProfileData = async () => {
loading.value = true;
try {
const response = await client.post(`user/${me.value.id}`, {
email: email.value,
username: username.value
})
if (response.status === 200) {
me.value = response.data
setLogin(me.value)
alert('Speichern war erfolgreich')
} else {
alert('Speichern war nicht erfolgreich')
}
} catch (error) {
console.error(error)
} finally {
loading.value = false;
}
}
const onSubmitChangePassword = async () => {
loading.value = true;
try {
const response = await client.post(`user/${me.value.id}/password`, {
oldPassword: oldPassword.value,
newPassword: newPassword.value
})
if (response.status === 200) {
alert('Passwort wurde geändert')
} else {
alert('Passwort konnte nicht geändert werden')
}
} catch (error) {
console.error(error)
} finally {
loading.value = false;
}
}
loadUser()
</script>
<template>
<h1>Profil Verwalten</h1>
<form @submit.prevent="onSubmitProfileData">
<div class="field">
<label>E-Mail-Adresse</label>
<InputText class="w-full" v-model="email" type="email" required />
</div>
<div class="field">
<label>Benutzer (andere Benutzer aus dem selben Track können diesen sehen)</label>
<InputText class="w-full" v-model="username" type="text" required />
</div>
<Button :disabled="loading" type="submit" label="Speichern" />
</form>

<h2>Passwort Ändern</h2>

<form @submit.prevent="onSubmitChangePassword">
<div class="field">
<label>Altes Passwort</label>
<InputText class="w-full" v-model="oldPassword" type="password" required />
</div>
<div class="field">
<label>Neues Passwort</label>
<InputText class="w-full" v-model="newPassword" type="password" required />
</div>
<Button :disabled="loading" type="submit" label="Speichern" />
</form>
<hr />
<h3>RSA Keypair generieren</h3>
<Button @click="onGenerateKeypair" label="Generieren (ausgabe in der Konsole)" />
<h3>Profildaten Server</h3>
<pre>{{ me }}</pre>
<h3>Profildaten Browser Session</h3>
<pre>{{ user }}</pre>
</template>

0 comments on commit 6f9cba8

Please sign in to comment.