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'
|
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
|
||||||
export default class EventsController {
|
export default class EventsController {
|
||||||
public async index({}: HttpContextContract) {}
|
public async index({ inertia }: HttpContextContract) {
|
||||||
|
return inertia.render('Events/Index')
|
||||||
|
}
|
||||||
|
|
||||||
public async create({}: HttpContextContract) {}
|
public async create({}: HttpContextContract) {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
import User from 'App/Models/User'
|
||||||
|
|
||||||
export default class UsersController {
|
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) {}
|
public async store({}: HttpContextContract) {}
|
||||||
|
|
||||||
@@ -13,5 +22,17 @@ export default class UsersController {
|
|||||||
|
|
||||||
public async update({}: HttpContextContract) {}
|
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"
|
ref="formRef"
|
||||||
:rules="rules"
|
:rules="rules"
|
||||||
>
|
>
|
||||||
<n-form-item label="Benutzername" path="user">
|
<n-form-item label="Benutzername" path="username">
|
||||||
<n-input v-model:value="formValue.user" placeholder="Benutzername" />
|
<n-input v-model:value="formValue.username" placeholder="Benutzername" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
<n-form-item label="Passwort" path="password">
|
<n-form-item label="Passwort" path="password">
|
||||||
<n-input v-model:value="formValue.password" placeholder="Passwort"></n-input>
|
<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>
|
<n-button class="bg-[#18A058]" type="success" @click="onClickLogin">Anmelden</n-button>
|
||||||
</div>
|
</div>
|
||||||
</n-form>
|
</n-form>
|
||||||
|
<div>
|
||||||
|
{{ response }}
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { ref, reactive } from 'vue'
|
||||||
import { router } from '@inertiajs/vue3'
|
import { router } from '@inertiajs/vue3'
|
||||||
import LoginLayout from '@/layouts/LoginLayout.vue'
|
import LoginLayout from '@/layouts/LoginLayout.vue'
|
||||||
|
|
||||||
defineOptions({ layout: LoginLayout })
|
defineOptions({ layout: LoginLayout })
|
||||||
|
const props = defineProps(['response'])
|
||||||
|
|
||||||
const formRef = ref(null)
|
const formRef = ref(null)
|
||||||
const formValue = ref({
|
const formValue = reactive({
|
||||||
user: '',
|
username: '',
|
||||||
password: ''
|
password: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
const rules = ref(undefined)
|
const rules = ref(undefined)
|
||||||
|
|
||||||
function onClickLogin(){
|
function onClickLogin(){
|
||||||
router.get(
|
router.post('/login', formValue)
|
||||||
'/events'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -19,13 +19,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Route from '@ioc:Adonis/Core/Route'
|
import Route from '@ioc:Adonis/Core/Route'
|
||||||
|
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
|
||||||
Route.get('/', 'HomeController.index')
|
Route.get('/', 'HomeController.index')
|
||||||
|
|
||||||
Route.get('/login', async({inertia}) =>{
|
Route.get('/login', async({inertia, response}: HttpContextContract) =>{
|
||||||
return inertia.render('Login')
|
return inertia.render('Login')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Route.post('login', 'AuthController.login')
|
||||||
|
|
||||||
Route.resource('users', 'UsersController').middleware({
|
Route.resource('users', 'UsersController').middleware({
|
||||||
"*": ['auth']
|
"*": ['auth']
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user