Once user sign up, we store the user data inside cookie in the broswer and also keep a memory copy in the server.

If next time, user refresh the page, we want to tell that the user is already authed.

Create a endpoint, to retrive the user data:

app.route('/api/user')
.get(getUser);

Router:

import {Request, Response} from 'express';
import {sessionStore} from './session-store'; export function getUser(req: Request, res: Response) {
// Get sessionid from cookies
const sessionId = req.cookies['SESSIONID'];
// get user according to the session id from the session storage
const user = sessionStore.findUserBySessionId(sessionId);
if (user) {
// if there is user, send successful response
res.status(200).json(user);
} else {
// if there is no user, send empty response
res.sendStatus(204);
}
}

SessionStorage:

import {Session} from './session';
import {User} from '../src/app/model/user';
class SessionStore {
private sessions: {[key: string]: Session} = {}; createSession(sessionId: string, user: User) {
this.sessions[sessionId] = new Session(sessionId, user);
} findUserBySessionId(sessionId: string): User | undefined {
const session = this.sessions[sessionId];
const isSessionValid = session && session.isValid();
return isSessionValid ? session.user : undefined;
}
} // We want only global singleton
export const sessionStore = new SessionStore();

On the client, once page loaded, we try to get user data first.

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {User} from '../model/user';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/shareReplay';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/do'; export const ANONYMOUS_USER: User = {
id: undefined,
email: ''
}; @Injectable()
export class AuthService { subject = new BehaviorSubject<User>(undefined);
// filter out undefined user
user$: Observable<User> = this.subject.asObservable().filter(user => !!user);
isLoggedIn$: Observable<boolean> = this.user$.map(user => !!user.id);
isLoggedOut$: Observable<boolean> = this.isLoggedIn$.map(isLoggedIn => !isLoggedIn); constructor(private http: HttpClient) {
this.http.get<User>('/api/user')
// when there is valid session id, emit the user$
.subscribe((user) => this.subject.next(user ? user : ANONYMOUS_USER));
} signUp(email: string, password: string) {
return this.http.post<User>('/api/signup', {
email,
password
}).shareReplay()
.do((user) => this.subject.next(user));
} }

[Angular & Web] Retrieve user data from Session的更多相关文章

  1. web跨域访问,session丢失的问题

    web跨域访问,session丢失的问题25 http://www.iteye.com/problems/71265 http://www.iteye.com/topic/264079 具体情况如下: ...

  2. 如何在web.config文件中配置Session变量的生命周期

    实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...

  3. web.py之cookie和session

    官方给的session例子这里就不讲了.下面直接将怎么设置session,取session: session相关代码一定要放在web.py框架的Main.py里面. # Main.py # 设置ses ...

  4. Java Web学习(五)session、cookie、token

    文章更新时间:2020/09/14 一.引言 动态网页兴起后,会话管理变成开发者需要考虑的一个问题,由于HTTP请求是无状态的,为了区分每个用户,此时引入了会话标识(sessionId)的概念,但是存 ...

  5. ionic2+Angular web端 实现微信分享以及如何跳转回分享出去的页面

    微信分享,首先参考微信JS-SDK开发文档. step1:在启动文件index.html中引入微信js文件: <script src="http://res.wx.qq.com/ope ...

  6. java web学习总结(十二) -------------------Session

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  7. java web 学习十二(session)

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  8. Python Web学习笔记之Cookie,Session,Token区别

    一.Cookie,Session,Token简介 # 这三者都解决了HTTP协议无状态的问题 session ID or session token is a piece of data that i ...

  9. Web APi 2.0优点和特点?在Web APi中如何启动Session状态?

    前言 曾几何时,微软基于Web服务技术给出最流行的基于XML且以扩展名为.asmx结尾的Web Service,此服务在.NET Framework中风靡一时同时也被.NET业界同仁所青睐,几年后在此 ...

随机推荐

  1. rm---删除目录huo文件

    rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉.对于链接文件,只是删除整个链接文件,而原有文件保持不变. 注意:使用rm命令要格外小心.因为一旦 ...

  2. 【Henu ACM Round#16 D】Bear and Two Paths

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先搞一条a到b的路径 a c x3 x4 x5....xn-2 d b 然后第二个人的路径可以这样 c a x3 x4 x5...x ...

  3. AFNetworking 取消请求

    取消单个操作: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ...

  4. button-xml 中android:clickable="false" 属性

    今天在做项目的时候,遇到了一个问题,就是需要把一个常按监听事件,加到一个linearlayout中,但是,这个linearlayout中有其他的button.textview等控件,这样就导致当我们常 ...

  5. selection-内容选中跟光标移动

    如果我们希望手动的改变edittext的光标,我们可以使用 setSelection(int start, int end); setSelection(int index); 这个方法,如果我们选择 ...

  6. webservices 服务器未能识别 HTTP 头 SOAPAction 的值:.

    转自:https://blog.csdn.net/dxfasr/article/details/25029063 在用java发送给webservice服务器的时候报如下错误: AxisFault f ...

  7. POJ 2458 DFS+判重

    题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...

  8. last---显示用户最近登录信息

    last命令用于显示用户最近登录信息.单独执行last命令,它会读取/var/log/wtmp的文件,并把该给文件的内容记录的登入系统的用户名单全部显示出来. 语法 last(选项)(参数) 选项 - ...

  9. Visual Studio Code配置GoLang开发环境

    Visual Studio Code配置GoLang开发环境 在Visual Studio Code配置GoLang开发环境 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页: ...

  10. 25.C++多线程

    #include <iostream> #include <thread> #include <Windows.h> using namespace std; vo ...