right now I am registering users in local storage and logging them in through local storage. How to make that switch to DB instead without the use of API?
user registration ts file:
onSubmit() {
this.userSubmitted = true;
if (this.registerationForm.valid)
{
if (this.adminPass.value === environment.adminPass){
this.UserServiceService.addUser(this.userData());
this.registerationForm.reset();
this.userSubmitted = false;
this.alertify.success('You have registered successfully!');
}
else{
this.alertify.error('You need to be an admin! Enter the correct admin password!');
}
}
else{
this.alertify.error('Please fill all required fields!');
}
}
and the service.ts file:
import { Injectable } from '@angular/core';
import { User } from '../model/user';
@Injectable({
providedIn: 'root'
})
export class UserServiceService {
constructor() { }
addUser(user: User) {
let users = [];
if(localStorage.getItem('Users'))
{
users = JSON.parse(localStorage.getItem('Users') as string);
users = [user, ...users];
}
else
{
users = [user];
}
localStorage.setItem('Users', JSON.stringify(users))
}
}
in login page:
onlogin(loginForm: NgForm){
console.log(loginForm.value);
const token = this.authServive.authUser(loginForm.value);
if(token)
{
localStorage.setItem('token', token.userName);
this.alertify.success("You have loged-in successfully!");
this.router.navigate(['/']);
}
else
{
this.alertify.error("Username and/or Password is not corrent!");
}
}
}
and this is the authUser
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor() { }
authUser(user: any)
{
let UserArray = [];
if (localStorage.getItem("Users"))
{
UserArray = JSON.parse(localStorage.getItem("Users") || '{}');
}
return UserArray.find((p: { userName: any; password: any; }) => p.userName === user.userName && p.password === user.password);
}
}
how to convert that to using a database such as sql without the help of API