0

I want to store the id of user in local storage so I can get it and print the data of that particular user from the database, I have added the node backend and front end of login page

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
import { AuthService } from '../auth.service';

@Component({
    selector: 'login-form',
    templateUrl: './login-form.component.html',
    styleUrls: ['./login-form.component.css']
})
export class LoginFormComponent implements OnInit {
    username: any;
    password: any;
    form: FormGroup;
    id: number;
    constructor(private router: Router, private formBuilder: FormBuilder, private authService: AuthService) {
    }

    ngOnInit() {
        this.initf();
    }

    initf() {
        this.form = this.formBuilder.group({
        username: this.username,
        password: this.password
        });
    }
    log() {
        console.log(this.username, this.password);
        var user = {username:this.username, password:this.password , user:this.id}
        this.authService.login(user).subscribe(res={
        if(res != '400'){
            localStorage.setItem(user =this.id, 'id');
            this.router.navigate(['todoapp']);
        }
        })
    }
}

My API

app.post('/api/user/login', (req, res) = {
    var user = req.body
    var sql = `SELECT * FROM user where username = '${user.username}' AND password = '${user.password} limit 1'`;
    mysqlConnection.query(sql, (err, row) = {
        if (!err)
            if(row.length  0){
                res.send(row)
            }else{
                res.send('400')
            }

        else
            console.log(err);
    })
});
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Malik Shafi
  • 251
  • 2
  • 4
  • 12

0 Answers0