Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

node.js - why req.logIn () doesn't store data when using angular?

I am working on angular and nodejs and their libraries, passport, passport-local, express, express-session. the req.logIn () does not save the data to the session, I tried the rest api with postman and it works correctly, and it saves the data, what is happening ?. could someone help please thank you in advance?

this is the code that according to passport closes the session:

app.get('/logout', (req, res) => {
        req.logout();
        res.redirect('logout');
 });

add cors handling to be able to communicate with another port and activate the credentials for sending the cookie:

app.use(cors({origin:["http://localhost:4200"],credentials:true}));

const store = new session.MemoryStore; *//session configuration*
app.use(session({
    secret:"SECRET",
    resave:true,
    saveUninitialized:true,
    cookie:{
        secure: false
    },
    store:store
}))
app.use(passport.initialize());*//initialize passport and passport sessions*
app.use(passport.session());

the login part is like this:

    exports.Login = (req, res, next) => {
    passport.authenticate('local', (err, usuario, info) => {
        if (err) {
            next(err)
        }
        if (!usuario) {
            res.json(info)
        }
        else {
            req.logIn(usuario, (err) => {
                if (err) {
                    next(err)
                }
                else {
                    res.json("usuario existente"})
                    next()
                }
            })
        }
    })(req,res, next)
}
question from:https://stackoverflow.com/questions/65890437/why-req-login-doesnt-store-data-when-using-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you tried with Postman and it worked and it means Express is working fine. so you need to check your Angular side.

because you are working with Session you need to set the correct setting:

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable()
export class SessionInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const newRequest = request.clone({
      withCredentials: true
    });
    return next.handle(newRequest);
  }
}

import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [],
  imports: [],
  providers: [ { provide: HTTP_INTERCEPTORS, useClass: SessionInterceptor, multi: true } ],
  bootstrap: [AppComponent]
})
export class AppModule {}

more info:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials

Angular 6 httpClient Post with credentials


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...