working on AuthController
This commit is contained in:
@@ -1,3 +1,36 @@
|
||||
// import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
export default class AuthController {}
|
||||
export default class AuthController {
|
||||
public async login({ auth, request, session, response }: HttpContextContract){
|
||||
|
||||
const { username, password } = request.all()
|
||||
/*const loginSchema = schema.create({
|
||||
username: schema.string({trim: true}),
|
||||
password: schema.string()
|
||||
})
|
||||
|
||||
const { username, password } = await request.validate({
|
||||
schema: loginSchema,
|
||||
messages: {
|
||||
required: 'This field is required'
|
||||
}
|
||||
})*/
|
||||
|
||||
try {
|
||||
await auth.attempt(username, password)
|
||||
|
||||
response.redirect('/events')
|
||||
} catch (error) {
|
||||
session.flash("notification", "Login fehlgeschlagen")
|
||||
response.redirect("/")
|
||||
}
|
||||
}
|
||||
|
||||
public async logout({ auth, response }: HttpContextContract) {
|
||||
await auth.logout()
|
||||
|
||||
response.redirect('/login')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class EventsController {
|
||||
public async index({}: HttpContextContract) {}
|
||||
public async index({ inertia }: HttpContextContract) {
|
||||
return inertia.render('Events/Index')
|
||||
}
|
||||
|
||||
public async create({}: HttpContextContract) {}
|
||||
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from 'App/Models/User'
|
||||
|
||||
export default class UsersController {
|
||||
public async index({}: HttpContextContract) {}
|
||||
public async index({}: HttpContextContract) {
|
||||
|
||||
public async create({}: HttpContextContract) {}
|
||||
}
|
||||
|
||||
public async create({ auth, inertia }: HttpContextContract) {
|
||||
if(auth.user?.isAdmin) {
|
||||
inertia.render('Users/Create')
|
||||
} else {
|
||||
inertia
|
||||
}
|
||||
}
|
||||
|
||||
public async store({}: HttpContextContract) {}
|
||||
|
||||
@@ -13,5 +22,17 @@ export default class UsersController {
|
||||
|
||||
public async update({}: HttpContextContract) {}
|
||||
|
||||
public async destroy({}: HttpContextContract) {}
|
||||
public async destroy({ auth, response, params }: HttpContextContract) {
|
||||
if (auth.user?.isAdmin){
|
||||
const user = await User.findOrFail(params.id)
|
||||
await user.delete()
|
||||
.then(
|
||||
() => {
|
||||
response.redirect('users.index')
|
||||
},
|
||||
)
|
||||
// TODO implement reasonable error handling.
|
||||
.catch(error => console.log(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
ref="formRef"
|
||||
:rules="rules"
|
||||
>
|
||||
<n-form-item label="Benutzername" path="user">
|
||||
<n-input v-model:value="formValue.user" placeholder="Benutzername" />
|
||||
<n-form-item label="Benutzername" path="username">
|
||||
<n-input v-model:value="formValue.username" placeholder="Benutzername" />
|
||||
</n-form-item>
|
||||
<n-form-item label="Passwort" path="password">
|
||||
<n-input v-model:value="formValue.password" placeholder="Passwort"></n-input>
|
||||
@@ -16,26 +16,28 @@
|
||||
<n-button class="bg-[#18A058]" type="success" @click="onClickLogin">Anmelden</n-button>
|
||||
</div>
|
||||
</n-form>
|
||||
<div>
|
||||
{{ response }}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
import { ref, reactive } from 'vue'
|
||||
import { router } from '@inertiajs/vue3'
|
||||
import LoginLayout from '@/layouts/LoginLayout.vue'
|
||||
|
||||
defineOptions({ layout: LoginLayout })
|
||||
const props = defineProps(['response'])
|
||||
|
||||
const formRef = ref(null)
|
||||
const formValue = ref({
|
||||
user: '',
|
||||
password: ''
|
||||
const formValue = reactive({
|
||||
username: '',
|
||||
password: '',
|
||||
})
|
||||
|
||||
const rules = ref(undefined)
|
||||
|
||||
function onClickLogin(){
|
||||
router.get(
|
||||
'/events'
|
||||
)
|
||||
router.post('/login', formValue)
|
||||
}
|
||||
</script>
|
||||
@@ -19,13 +19,16 @@
|
||||
*/
|
||||
|
||||
import Route from '@ioc:Adonis/Core/Route'
|
||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
Route.get('/', 'HomeController.index')
|
||||
|
||||
Route.get('/login', async({inertia}) =>{
|
||||
Route.get('/login', async({inertia, response}: HttpContextContract) =>{
|
||||
return inertia.render('Login')
|
||||
})
|
||||
|
||||
Route.post('login', 'AuthController.login')
|
||||
|
||||
Route.resource('users', 'UsersController').middleware({
|
||||
"*": ['auth']
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user