Skip to content

Commit

Permalink
add auth
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Oct 19, 2024
1 parent 1bdc050 commit 2bc947d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 1 deletion.
132 changes: 132 additions & 0 deletions components/admin/LoginPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<template>
<div class="login">
<div class="login__header">
<h1 class="login__title">Login</h1>
</div>
<form @submit.prevent="handleLogin" class="login__form">
<div class="login__input-group">
<label for="username" class="login__label">Username</label>
<input
id="username"
type="username"
v-model="username"
placeholder="Enter your username"
class="login__input"
required
/>
</div>
<div class="login__input-group">
<label for="password" class="login__label">Password</label>
<input
id="password"
type="password"
v-model="password"
placeholder="Enter your password"
class="login__input"
required
/>
</div>
<button type="submit" class="login__submit">Login</button>
</form>
</div>
</template>

<script>
import { useFetch } from 'nuxt/app'
export default {
name: 'LoginPage',
data() {
return {
username: '',
password: '',
}
},
methods: {
async handleLogin() {
try {
const { data, error } = await useFetch('/api/login', {
method: 'POST',
body: { username: this.username, password: this.password },
headers: {
'Content-Type': 'application/json',
},
})
if (error.value) {
this.errorMessage = 'Invalid login credentials. Please try again.'
return
}
// Redirect to another page or store user data, etc.
} catch (err) {
this.errorMessage = 'Something went wrong. Please try again later.'
console.error(err)
}
}
}
}
</script>

<style lang="scss" scoped>
.login {
&__header {
margin-bottom: $shortMargin;
text-align: center;
}
&__title {
font-size: 24px;
font-weight: bold;
margin-bottom: $halfShortMargin;
}
&__form {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid rgb(231, 226, 222);
border-radius: 8px;
background-color: #fff;
}
&__input-group {
margin-bottom: $halfShortMargin;
@media (min-width: 1023px) {
margin-bottom: $shortMargin;
}
}
&__label {
display: block;
font-weight: bold;
margin-bottom: 8px;
}
&__input {
width: 100%;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
&__submit {
width: 100%;
padding: 12px;
background-color: $colorMain;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
transition: background-color $defaultAnimTime;
&:hover {
background-color: $colorHover;
}
}
}
</style>
32 changes: 32 additions & 0 deletions pages/admin/login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<template>
<main class="wrapper">
<LoginPage />
</main>
</template>

<script>
import { defineComponent } from 'vue'
import { useHead } from 'nuxt/app'
import LoginPage from '~~/components/admin/LoginPage.vue'
export default defineComponent({
name: 'LoginPageWrapper',
components: { LoginPage },
setup() {
// Optionally, you can set meta tags or SEO related content.
useHead({
meta: [{ name: 'robots', content: 'noindex, nofollow' }]
})
return {}
}
})
</script>

<style lang="scss" scoped>
.wrapper {
@include defaultWrapper;
}
</style>
2 changes: 2 additions & 0 deletions server/api/login.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineEventHandler(async (event) => {
const body = await readBody(event)
const { username, password } = body

console.log('username:', username)

const { sessionId, expiresAt, error } = await addSessionToUser(
username,
password
Expand Down
4 changes: 3 additions & 1 deletion server/api/service.get.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { defineEventHandler } from 'h3'
import { db } from '~/server/utils/mongo'

export default defineEventHandler(async () => {
export default defineEventHandler(async (event) => {
const response = {}
if (!db) {
response.isDbConnected = false
} else {
response.isDbConnected = true
}
const safeUsername = event.context?.auth?.username || 'User not logged in'
response.username = safeUsername
return response
})
13 changes: 13 additions & 0 deletions server/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineEventHandler, parseCookies } from 'h3'
import { getUserBySessionId } from '~/server/utils/user'

export default defineEventHandler(async (event) => {
const cookie = parseCookies(event)
const sessionId = cookie.sessionId
const user = await getUserBySessionId(sessionId)

if (user) {
event.context.auth = { username: user.username }
}
})

0 comments on commit 2bc947d

Please sign in to comment.