如何将用户ID存储在本地存储中,以便可以从数据库中检索数据

时间:2019-08-09 12:03:43

标签: angular typescript local-storage

我想将用户ID存储在本地存储中,以便获取它并从数据库中打印该特定用户的数据,我已经添加了登录页面的后端和前端节点

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']);
        }
        })
    }
}

我的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);
    })
});

0 个答案:

没有答案