Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
arsnova-lite
Manage
Activity
Members
Labels
Plan
Issues
24
Issue boards
Milestones
Wiki
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Privacy
Imprint
Contact
Snippets
Groups
Projects
Show more breadcrumbs
ARSnova
arsnova-lite
Commits
c18c5838
Commit
c18c5838
authored
7 years ago
by
David Noah Donges
Browse files
Options
Downloads
Patches
Plain Diff
Persist user data
Store and load user data so we don't lose the user's data on reload.
parent
99c864eb
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!58
Persist user data
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/app/authentication.service.ts
+20
-8
20 additions, 8 deletions
src/app/authentication.service.ts
with
20 additions
and
8 deletions
src/app/authentication.service.ts
+
20
−
8
View file @
c18c5838
...
...
@@ -3,17 +3,27 @@ import { User } from './user';
import
{
Observable
}
from
'
rxjs/Observable
'
;
import
{
of
}
from
'
rxjs/observable/of
'
;
import
{
UserRole
}
from
'
./user-roles.enum
'
;
import
{
DataStoreService
}
from
'
./data-store.service
'
;
// TODO: connect to API
// TODO: persist user data (shouldn't get lost on page refresh)
@
Injectable
()
export
class
AuthenticationService
{
private
mockUser
:
User
;
constructor
()
{
}
private
readonly
STORAGE_KEY
:
string
=
'
USER
'
;
private
user
:
User
;
constructor
(
private
dataStoreService
:
DataStoreService
)
{
if
(
dataStoreService
.
has
(
this
.
STORAGE_KEY
))
{
// Load user data from local data store if available
this
.
user
=
JSON
.
parse
(
dataStoreService
.
get
(
this
.
STORAGE_KEY
));
}
}
login
(
email
:
string
,
password
:
string
,
role
:
UserRole
):
Observable
<
boolean
>
{
this
.
mockUser
=
new
User
(
1
,
''
,
email
,
role
);
this
.
user
=
new
User
(
1
,
''
,
email
,
role
,
'
TOKEN
'
);
// Store user data in local storage to retain the data when the user reloads the page
this
.
dataStoreService
.
set
(
this
.
STORAGE_KEY
,
JSON
.
stringify
(
this
.
user
));
return
of
(
true
);
}
...
...
@@ -26,19 +36,21 @@ export class AuthenticationService {
}
logout
()
{
this
.
mockUser
=
null
;
// Destroy the persisted user data
this
.
dataStoreService
.
remove
(
this
.
STORAGE_KEY
);
this
.
user
=
undefined
;
}
getUser
():
Observable
<
User
>
{
return
of
(
this
.
mockU
ser
);
return
of
(
this
.
u
ser
);
}
isLoggedIn
():
Observable
<
boolean
>
{
return
of
(
this
.
mockU
ser
!==
undefined
);
return
of
(
this
.
u
ser
!==
undefined
);
}
getRole
():
Observable
<
UserRole
>
{
return
of
(
this
.
mockU
ser
.
role
);
return
of
(
this
.
u
ser
.
role
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
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