@@ -38,20 +38,36 @@ const routes = (app) => {
38
38
39
39
app . get ( '/*' , ( req , res ) => {
40
40
const content = middleware . fileSystem . readFileSync ( path . join ( __dirname , '../../../dist/index.html' ) ) ;
41
- const key = '<div id="root"></div>' ;
42
- const index = content . indexOf ( key ) + key . length ;
43
- const inject = req . user ? `<script type="text/javascript">window.id="${ req . user . _id } ";window.admin=${ req . user . isAdmin } ;</script>` : '' ;
44
- res . send ( content . slice ( 0 , index ) + inject + content . slice ( index ) ) ;
41
+
42
+ if ( req . user ) {
43
+ res . cookie ( 'id' , req . user . _id . toString ( ) , { path : '/' } ) ;
44
+ if ( req . user . isAdmin ) {
45
+ res . cookie ( 'admin' , req . user . isAdmin , { path : '/' } ) ;
46
+ }
47
+ } else {
48
+ res . clearCookie ( 'admin' , { path : '/' } ) ;
49
+ res . clearCookie ( 'id' , { path : '/' } ) ;
50
+ }
51
+
52
+ res . set ( 'Content-Type' , 'text/html' ) ;
53
+ res . send ( content ) ;
45
54
} ) ;
46
55
} else {
47
56
app . use ( '/dist' , express . static ( path . join ( __dirname , '../../../dist' ) ) ) ;
48
57
const content = fs . readFileSync ( path . join ( __dirname , '../../../dist/index.html' ) , 'utf8' ) ;
49
- const key = '<div id=root></div>' ;
50
- const index = content . indexOf ( key ) + key . length ;
51
58
52
59
app . get ( '/*' , ( req , res ) => {
53
- const inject = req . user ? `<script type="text/javascript">window.id="${ req . user . _id } ";window.admin=${ req . user . isAdmin } ;</script>` : '' ;
54
- res . send ( content . slice ( 0 , index ) + inject + content . slice ( index ) ) ;
60
+ if ( req . user ) {
61
+ res . cookie ( 'id' , req . user . _id . toString ( ) , { path : '/' } ) ;
62
+ if ( req . user . isAdmin ) {
63
+ res . cookie ( 'admin' , req . user . isAdmin , { path : '/' } ) ;
64
+ }
65
+ } else {
66
+ res . clearCookie ( 'admin' , { path : '/' } ) ;
67
+ res . clearCookie ( 'id' , { path : '/' } ) ;
68
+ }
69
+ res . set ( 'Content-Type' , 'text/html' ) ;
70
+ res . send ( content ) ;
55
71
} ) ;
56
72
}
57
73
} ;
0 commit comments