Home > Net >  Save service data while refreshing page
Save service data while refreshing page

Time:12-30

I have a simple service, that holds user credential ( username and password via btoa ):

@Injectable({
  providedIn: 'root'
})
export class UserService {
  userCredentials = '';
}

initializing fileds while loggin in:

this.userService.userCredentials =  btoa(this.user.username   ':'   this.user.password);

I use this credentials in my backend spring boot app, to authenticate user etc.:

@RequestMapping("/core/user")
public Principal user(HttpServletRequest request) {
    String authToken = request.getHeader("Authorization").substring("Basic".length()).trim();
    return () -> new String(Base64.getDecoder().decode(authToken)).split(":")[0];
}

How to handle page refresh in such way as not to lose data in service? Holding such data in session storage is very unsafe, so I don't want to store user credential in localStorage/sessionStorage.

CodePudding user response:

You should save your authToken, not the userCredentials. On refresh, make a query to log in with the token. If it's still valid, you'll retrieve user credentials.

CodePudding user response:

Basically you can not do that. You can use localStorage, sessionStorage, cookies, pass them to queryParams on reload and get them again or get them by a api call.

No other way.

  • Related