Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Auth = {
- users: [],
- currentUser: null,
- start() {
- let database = localStorage.getItem('users');
- let currentUser = localStorage.getItem('currentUser');
- if (database) {
- this.users = JSON.parse(database);
- }
- if (currentUser) {
- this.currentUser = JSON.parse(currentUser);
- }
- },
- login(data) {
- let user = this.getUserByName(data.name);
- if (user) {
- let passwordMatches = this.checkPassword(data.password, user);
- if (false === passwordMatches) {
- return;
- }
- this.currentUser = user;
- localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
- } else {
- this.users.push(data);
- this.currentUser = data;
- localStorage.setItem('users', JSON.stringify(this.users));
- localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
- }
- return this.currentUser;
- },
- getUserByName(name) {
- return this.users.find((user) => name === user.name);
- },
- checkPassword(password, user) {
- return password === user.password ? true : false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement