-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check isUserLoggedIn without a network round-trip
This improves the first page load time (one less network round-trip in most cases).
- Loading branch information
Viktor Lukashov
committed
Nov 30, 2020
1 parent
f36686c
commit 4611870
Showing
6 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { login as loginImpl, logout as logoutImpl } from '@vaadin/flow-frontend'; | ||
import type { LoginResult } from '@vaadin/flow-frontend'; | ||
|
||
const LAST_LOGIN_TIMESTAMP = 'lastLoginTimestamp'; | ||
const THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1000; | ||
const lastLoginTimestamp = localStorage.getItem(LAST_LOGIN_TIMESTAMP); | ||
const hasRecentLoginTimestamp = (lastLoginTimestamp && | ||
(new Date().getTime() - new Date(+lastLoginTimestamp).getTime()) < THIRTY_DAYS_MS) || false; | ||
|
||
let _isLoggedIn = hasRecentLoginTimestamp; | ||
|
||
export async function login(username: string, password: string): Promise<LoginResult> { | ||
if (_isLoggedIn) { | ||
return { error: false } as LoginResult; | ||
} else { | ||
const result = await loginImpl(username, password); | ||
if (!result.error) { | ||
_isLoggedIn = true; | ||
localStorage.setItem(LAST_LOGIN_TIMESTAMP, new Date().getTime() + '') | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
export async function logout() { | ||
_isLoggedIn = false; | ||
localStorage.removeItem(LAST_LOGIN_TIMESTAMP); | ||
return await logoutImpl(); | ||
} | ||
|
||
export function isLoggedIn() { | ||
return _isLoggedIn; | ||
} | ||
|
||
export function setSessionExpired() { | ||
_isLoggedIn = false; | ||
localStorage.removeItem(LAST_LOGIN_TIMESTAMP); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import {ConnectClient, InvalidSessionMiddleware} from '@vaadin/flow-frontend'; | ||
import {setSessionExpired} from './auth'; | ||
const client = new ConnectClient({prefix: 'connect', middlewares: [new InvalidSessionMiddleware( | ||
async () => { | ||
const {LoginView} = await import ('./components/login-view/login-view'); | ||
return new LoginView().showOverlay(); | ||
setSessionExpired(); | ||
const {LoginView} = await import ('./components/login-view/login-view'); | ||
return LoginView.showOverlay(); | ||
} | ||
)]}); | ||
export default client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/main/java/com/vaadin/tutorial/crm/backend/service/endpoint/SecurityEndpoint.java
This file was deleted.
Oops, something went wrong.