Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106-2024-07
frontend
Commits
2c3a4253
Commit
2c3a4253
authored
11 months ago
by
VIktorGrev
Browse files
Options
Downloads
Patches
Plain Diff
feat: Adding Cookie userstore
parent
660daef0
No related branches found
Branches containing commit
No related tags found
1 merge request
!5
User store
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/stores/UserStore.ts
+74
-0
74 additions, 0 deletions
src/stores/UserStore.ts
with
74 additions
and
0 deletions
src/stores/UserStore.ts
0 → 100644
+
74
−
0
View file @
2c3a4253
import
{
OpenAPI
}
from
'
@/api
'
;
import
Cookies
from
'
js-cookie
'
;
import
{
defineStore
}
from
'
pinia
'
;
const
cookiesStorage
:
Storage
=
{
setItem
(
key
,
state
)
{
return
Cookies
.
set
(
key
,
state
,
{
expires
:
3
});
},
getItem
(
key
)
{
const
store
=
Cookies
.
get
(
key
);
if
(
store
===
undefined
)
{
OpenAPI
.
TOKEN
=
''
;
return
''
;
}
OpenAPI
.
TOKEN
=
JSON
.
parse
(
Cookies
.
get
(
key
)
||
''
).
accessToken
;
return
Cookies
.
get
(
key
)
||
''
;
},
length
:
0
,
clear
:
function
():
void
{
Cookies
.
remove
(
'
userInfo
'
);
},
key
:
function
(
index
:
number
):
string
|
null
{
throw
new
Error
(
'
Function not implemented.
'
);
},
removeItem
:
function
(
key
:
string
):
void
{
throw
new
Error
(
'
Function not implemented.
'
);
},
};
export
type
UserStoreInfo
=
{
username
?:
string
;
firstname
?:
string
;
lastname
?:
string
;
accessToken
?:
string
;
role
?:
string
;
};
export
const
useUserInfoStore
=
defineStore
(
'
UserInfoStore
'
,
{
state
:
()
=>
({
username
:
''
,
firstname
:
''
,
lastname
:
''
,
accessToken
:
''
,
role
:
''
,
}),
actions
:
{
setUserInfo
(
userinfo
:
UserStoreInfo
)
{
userinfo
.
username
&&
(
this
.
$state
.
username
=
userinfo
.
username
);
userinfo
.
firstname
&&
(
this
.
$state
.
firstname
=
userinfo
.
firstname
);
userinfo
.
lastname
&&
(
this
.
$state
.
lastname
=
userinfo
.
lastname
);
userinfo
.
accessToken
&&
(
this
.
$state
.
accessToken
=
userinfo
.
accessToken
);
userinfo
.
accessToken
&&
(
OpenAPI
.
TOKEN
=
this
.
$state
.
accessToken
);
userinfo
.
role
&&
(
this
.
$state
.
role
=
userinfo
.
role
);
},
clearUserInfo
()
{
this
.
$state
.
username
=
''
;
this
.
$state
.
firstname
=
''
;
this
.
$state
.
lastname
=
''
;
this
.
$state
.
accessToken
=
''
;
this
.
$state
.
role
=
''
;
OpenAPI
.
TOKEN
=
undefined
;
},
},
getters
:
{
isLoggedIn
():
boolean
{
return
this
.
accessToken
!==
''
;
},
},
persist
:
{
enabled
:
true
,
strategies
:
[{
key
:
'
userInfo
'
,
storage
:
cookiesStorage
}],
},
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment