Skip to content

Commit d526aad

Browse files
author
Julien Neuhart
committed
adding better authentication system with login
1 parent c98a95d commit d526aad

File tree

10 files changed

+67
-34
lines changed

10 files changed

+67
-34
lines changed

app/assets/vue/App.vue

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<router-link class="nav-item" tag="li" to="/posts" active-class="active">
1414
<a class="nav-link">Posts</a>
1515
</router-link>
16+
<li class="nav-item" v-if="isAuthenticated">
17+
<a class="nav-link" href="/api/security/logout">Logout</a>
18+
</li>
1619
</ul>
1720
</div>
1821
</nav>
@@ -27,6 +30,12 @@
2730
2831
export default {
2932
name: 'app',
33+
beforeMount () {
34+
let vueRouting = this.$parent.$el.attributes['data-vue-routing'].value,
35+
queryParameters = JSON.parse(this.$parent.$el.attributes['data-query-parameters'].value);
36+
37+
router.push({path: vueRouting, query: queryParameters});
38+
},
3039
created () {
3140
axios.interceptors.response.use(undefined, (err) => {
3241
return new Promise(() => {
@@ -37,11 +46,10 @@
3746
});
3847
});
3948
},
40-
beforeMount () {
41-
let vueRouting = this.$parent.$el.attributes['data-vue-routing'].value,
42-
queryParameters = JSON.parse(this.$parent.$el.attributes['data-query-parameters'].value);
43-
44-
router.push({path: vueRouting, query: queryParameters});
49+
computed: {
50+
isAuthenticated () {
51+
return this.$store.getters['security/isAuthenticated']
52+
},
4553
},
4654
}
4755
</script>

app/assets/vue/api/security.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,4 @@ export default {
1010
}
1111
);
1212
},
13-
isAuthenticated () {
14-
return axios.get('/api/security/is-authenticated');
15-
},
1613
}

app/assets/vue/router/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ router.beforeEach((to, from, next) => {
2121
if (to.matched.some(record => record.meta.requiresAuth)) {
2222
// this route requires auth, check if logged in
2323
// if not, redirect to login page.
24-
store.dispatch('security/isAuthenticated')
25-
.then(() => {
26-
next();
27-
})
28-
.catch(() => {
29-
next({
30-
path: '/login',
31-
query: { redirect: to.fullPath }
32-
});
24+
console.log(document.cookie);
25+
console.log(store.getters['security/isAuthenticated']);
26+
if (store.getters['security/isAuthenticated']) {
27+
next();
28+
} else {
29+
next({
30+
path: '/login',
31+
query: { redirect: to.fullPath }
3332
});
33+
}
3434
} else {
3535
next(); // make sure to always call next()!
3636
}

app/assets/vue/store/security.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default {
55
state: {
66
isLoading: false,
77
error: null,
8+
isAuthenticated: false,
89
},
910
getters: {
1011
isLoading (state) {
@@ -16,19 +17,26 @@ export default {
1617
error (state) {
1718
return state.error;
1819
},
20+
isAuthenticated (state) {
21+
state.isAuthenticated = document.cookie.indexOf('authenticated') !== -1;
22+
return state.isAuthenticated;
23+
},
1924
},
2025
mutations: {
2126
['AUTHENTICATING'](state) {
2227
state.isLoading = true;
2328
state.error = null;
29+
state.isAuthenticated = false;
2430
},
2531
['AUTHENTICATING_SUCCESS'](state) {
2632
state.isLoading = false;
2733
state.error = null;
34+
state.isAuthenticated = true;
2835
},
2936
['AUTHENTICATING_ERROR'](state, error) {
3037
state.isLoading = false;
3138
state.error = error;
39+
state.isAuthenticated = false;
3240
},
3341
},
3442
actions: {
@@ -38,8 +46,5 @@ export default {
3846
.then(() => commit('AUTHENTICATING_SUCCESS'))
3947
.catch(err => commit('AUTHENTICATING_ERROR', err));
4048
},
41-
isAuthenticated () {
42-
return SecurityAPI.isAuthenticated();
43-
},
4449
},
4550
}

app/assets/vue/views/Login.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
6060
this.$store.dispatch('security/login', payload)
6161
.then(() => {
62-
if (typeof redirect !== 'undefined') {
62+
if (typeof redirect !== 'undefined') {
6363
this.$router.push({path: redirect});
64-
} else {
64+
} else {
6565
this.$router.push({path: '/home'});
66-
}
66+
}
6767
});
6868
},
6969
},

app/config/packages/framework.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ framework:
99
session:
1010
handler_id: session.handler.native_file
1111
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
12+
gc_maxlifetime: 300
1213

1314
#esi: true
1415
#fragments: true

app/config/packages/security.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ security:
2525

2626
logout:
2727
path: /api/security/logout
28+
handlers: [app.logout.handler]
2829

2930
logout_on_user_change: true
3031

app/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ services:
3434
class: App\Security\HashPasswordListener
3535
tags:
3636
- { name: doctrine.event_subscriber }
37+
38+
app.logout.handler:
39+
class: App\Security\LogoutHandler

app/src/Controller/ApiSecurityController.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace App\Controller;
44

55
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Cookie;
67
use Symfony\Component\HttpFoundation\JsonResponse;
78
use Symfony\Component\Routing\Annotation\Route;
8-
use FOS\RestBundle\Controller\Annotations as Rest;
99

1010
final class ApiSecurityController extends Controller
1111
{
@@ -15,7 +15,12 @@ final class ApiSecurityController extends Controller
1515
*/
1616
public function loginAction(): JsonResponse
1717
{
18-
return new JsonResponse('authenticated!');
18+
$securityCookie = new Cookie('authenticated', true, \time() + \intval(\ini_get('session.gc_maxlifetime')), '/', null, false, false);
19+
20+
$response = new JsonResponse('authenticated!');
21+
$response->headers->setCookie($securityCookie);
22+
23+
return $response;
1924
}
2025

2126
/**
@@ -26,13 +31,4 @@ public function logoutAction()
2631
{
2732
throw new \Exception('This should not be reached!');
2833
}
29-
30-
/**
31-
* @Rest\Get("/api/security/is-authenticated", name="isAuthenticated")
32-
* @return JsonResponse
33-
*/
34-
public function isAuthenticatedAction(): JsonResponse
35-
{
36-
return $this->isGranted('IS_AUTHENTICATED_FULLY') ? new JsonResponse('authenticated!') : new JsonResponse('not authenticated!', 401);
37-
}
3834
}

app/src/Security/LogoutHandler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Security;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8+
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
9+
10+
final class LogoutHandler implements LogoutHandlerInterface
11+
{
12+
/**
13+
* @param Request $request
14+
* @param Response $response
15+
* @param TokenInterface $token
16+
* @return void
17+
*/
18+
public function logout(Request $request, Response $response, TokenInterface $token): void
19+
{
20+
$response->headers->clearCookie('authenticated');
21+
}
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy