Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
greg
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
it-bott-integrasjoner
greg
Commits
eff921af
Commit
eff921af
authored
3 years ago
by
Stein Elgethun
Browse files
Options
Downloads
Patches
Plain Diff
Add utility functions to set and delete cookies
Issue: GREG-89
parent
2c436668
No related branches found
No related tags found
1 merge request
!140
Greg 89 login redirect
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/utils/index.test.ts
+29
-0
29 additions, 0 deletions
frontend/src/utils/index.test.ts
frontend/src/utils/index.ts
+10
-0
10 additions, 0 deletions
frontend/src/utils/index.ts
with
39 additions
and
0 deletions
frontend/src/utils/index.test.ts
+
29
−
0
View file @
eff921af
import
{
import
{
getCookie
,
getCookie
,
deleteCookie
,
setCookie
,
isValidEmail
,
isValidEmail
,
isValidFnr
,
isValidFnr
,
isValidMobilePhoneNumber
,
isValidMobilePhoneNumber
,
...
@@ -77,6 +79,33 @@ test('Get unknown cookie returns null', async () => {
...
@@ -77,6 +79,33 @@ test('Get unknown cookie returns null', async () => {
document
.
cookie
=
'
csrftoken= ; expires = Thu, 01 Jan 1970 00:00:00 GMT
'
document
.
cookie
=
'
csrftoken= ; expires = Thu, 01 Jan 1970 00:00:00 GMT
'
})
})
test
(
'
Get known cookie returns value
'
,
async
()
=>
{
document
.
cookie
=
'
key=value
'
expect
(
getCookie
(
'
key
'
)).
toEqual
(
'
value
'
)
document
.
cookie
=
'
key= ; expires = Thu, 01, Jan 1970 00:00:00 GMT
'
})
test
(
'
Deleting cookie removes it
'
,
async
()
=>
{
document
.
cookie
=
'
key=value
'
expect
(
getCookie
(
'
key
'
)).
toEqual
(
'
value
'
)
deleteCookie
(
'
key
'
)
expect
(
getCookie
(
'
key
'
)).
toEqual
(
null
)
})
test
(
'
setCookie creates a cookie with correct value
'
,
async
()
=>
{
setCookie
(
'
key
'
,
'
value
'
)
expect
(
getCookie
(
'
key
'
)).
toEqual
(
'
value
'
)
deleteCookie
(
'
key
'
)
})
test
(
'
setCookie overrides value that was set before
'
,
async
()
=>
{
setCookie
(
'
key
'
,
'
value
'
)
expect
(
getCookie
(
'
key
'
)).
toEqual
(
'
value
'
)
setCookie
(
'
key
'
,
'
differentvalue
'
)
expect
(
getCookie
(
'
key
'
)).
toEqual
(
'
differentvalue
'
)
deleteCookie
(
'
key
'
)
})
test
(
'
Valid fnr
'
,
async
()
=>
{
test
(
'
Valid fnr
'
,
async
()
=>
{
expect
(
isValidFnr
(
'
04026514903
'
)).
toEqual
(
true
)
expect
(
isValidFnr
(
'
04026514903
'
)).
toEqual
(
true
)
})
})
...
...
This diff is collapsed.
Click to expand it.
frontend/src/utils/index.ts
+
10
−
0
View file @
eff921af
...
@@ -23,6 +23,16 @@ export function getCookie(name: string) {
...
@@ -23,6 +23,16 @@ export function getCookie(name: string) {
return
decodeURIComponent
(
cookies
[
0
].
split
(
'
=
'
)[
1
])
return
decodeURIComponent
(
cookies
[
0
].
split
(
'
=
'
)[
1
])
}
}
export
function
setCookie
(
name
:
string
,
value
:
string
)
{
document
.
cookie
=
`
${
name
}
=
${
value
}
; path=/`
}
export
function
deleteCookie
(
name
:
string
)
{
if
(
getCookie
(
name
))
{
setCookie
(
name
,
'
; expires=Thu, 01 jan 1970 00:00:00 GMT
'
)
}
}
export
function
maybeCsrfToken
()
{
export
function
maybeCsrfToken
()
{
const
csrfToken
=
getCookie
(
'
csrftoken
'
)
const
csrfToken
=
getCookie
(
'
csrftoken
'
)
if
(
!
csrfToken
)
{
if
(
!
csrfToken
)
{
...
...
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