Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd526aad

Browse files
author
Julien Neuhart
committed
adding better authentication system with login
1 parentc98a95d commitd526aad

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-linkclass="nav-item"tag="li"to="/posts"active-class="active">
1414
<aclass="nav-link">Posts</a>
1515
</router-link>
16+
<liclass="nav-item"v-if="isAuthenticated">
17+
<aclass="nav-link"href="/api/security/logout">Logout</a>
18+
</li>
1619
</ul>
1720
</div>
1821
</nav>
@@ -27,6 +30,12 @@
2730
2831
exportdefault {
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
returnnewPromise(()=> {
@@ -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+
returnthis.$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-
returnaxios.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
returnstate.error;
1819
},
20+
isAuthenticated(state){
21+
state.isAuthenticated=document.cookie.indexOf('authenticated')!==-1;
22+
returnstate.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-
returnSecurityAPI.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
namespaceApp\Controller;
44

55
useSymfony\Bundle\FrameworkBundle\Controller\Controller;
6+
useSymfony\Component\HttpFoundation\Cookie;
67
useSymfony\Component\HttpFoundation\JsonResponse;
78
useSymfony\Component\Routing\Annotation\Route;
8-
useFOS\RestBundle\Controller\AnnotationsasRest;
99

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

2126
/**
@@ -26,13 +31,4 @@ public function logoutAction()
2631
{
2732
thrownew \Exception('This should not be reached!');
2833
}
29-
30-
/**
31-
* @Rest\Get("/api/security/is-authenticated", name="isAuthenticated")
32-
* @return JsonResponse
33-
*/
34-
publicfunctionisAuthenticatedAction():JsonResponse
35-
{
36-
return$this->isGranted('IS_AUTHENTICATED_FULLY') ?newJsonResponse('authenticated!') :newJsonResponse('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+
namespaceApp\Security;
4+
5+
useSymfony\Component\HttpFoundation\Request;
6+
useSymfony\Component\HttpFoundation\Response;
7+
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
8+
useSymfony\Component\Security\Http\Logout\LogoutHandlerInterface;
9+
10+
finalclass LogoutHandlerimplements LogoutHandlerInterface
11+
{
12+
/**
13+
* @param Request $request
14+
* @param Response $response
15+
* @param TokenInterface $token
16+
* @return void
17+
*/
18+
publicfunctionlogout(Request$request,Response$response,TokenInterface$token):void
19+
{
20+
$response->headers->clearCookie('authenticated');
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp