-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logout): client and server side
- Loading branch information
1 parent
e0e4230
commit 4ce890f
Showing
16 changed files
with
141 additions
and
49 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
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
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,3 +1,5 @@ | ||
export const CATALOGUE_DEFAULT_PAGE_SIZE = 4; | ||
export const CATALOGUE_DEFAULT_PAGE_NUMBER = 1; | ||
export const MINIMAL_PASSWORD_LENGTH = 2; | ||
|
||
export const COOKIE_JWT_KEY = "jwt"; |
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,34 @@ | ||
// https://gist.github.com/joduplessis/7b3b4340353760e945f972a69e855d11 | ||
|
||
/* | ||
* General utils for managing cookies in Typescript. | ||
*/ | ||
export function setCookie(name: string, val: string) { | ||
const date = new Date(); | ||
const value = val; | ||
|
||
// Set it expire in 7 days | ||
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000)); | ||
|
||
// Set it | ||
document.cookie = name+"="+value+"; expires="+date.toUTCString()+"; path=/"; | ||
} | ||
|
||
export function getCookie(name: string) { | ||
const value = "; " + document.cookie; | ||
const parts = value.split("; " + name + "="); | ||
|
||
if (parts.length == 2) { | ||
return parts.pop()?.split(";").shift(); | ||
} | ||
} | ||
|
||
export function deleteCookie(name: string) { | ||
const date = new Date(); | ||
|
||
// Set it expire in -1 days | ||
date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000)); | ||
|
||
// Set it | ||
document.cookie = name+"=; expires="+date.toUTCString()+"; path=/"; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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,3 +1,6 @@ | ||
package mu.muse.rest | ||
|
||
const val FAVORITE_INSTRUMENTS_SESSION_KEY = "FAVORITE_INSTRUMENTS" | ||
|
||
const val COOKIE_SESSION_ID = "SESSIONID" | ||
const val COOKIE_JWT_KEY = "jwt" |
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
36 changes: 36 additions & 0 deletions
36
server/app/src/main/kotlin/mu/muse/rest/logout/LogoutEndpoint.kt
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,36 @@ | ||
package mu.muse.rest.logout | ||
|
||
import jakarta.annotation.security.RolesAllowed | ||
import jakarta.servlet.http.Cookie | ||
import mu.muse.domain.user.Role | ||
import mu.muse.rest.COOKIE_JWT_KEY | ||
import mu.muse.rest.COOKIE_SESSION_ID | ||
import mu.muse.rest.api.LogoutApi | ||
import mu.muse.rest.login.BasicLoginEndpoint.Companion.COOKIE_PATH | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.context.request.RequestContextHolder | ||
import org.springframework.web.context.request.ServletRequestAttributes | ||
|
||
@RestController | ||
class LogoutEndpoint : LogoutApi { | ||
|
||
@RolesAllowed(Role.USER, Role.EDITOR) | ||
override fun logout(): ResponseEntity<Any> { | ||
val request = (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes).request | ||
|
||
val jwtKeyCookie = Cookie(COOKIE_JWT_KEY, "") | ||
jwtKeyCookie.maxAge = 0 | ||
jwtKeyCookie.path = COOKIE_PATH | ||
(RequestContextHolder.getRequestAttributes() as ServletRequestAttributes).response?.addCookie(jwtKeyCookie) | ||
|
||
request.session.invalidate() | ||
|
||
val sessionIdCookie = Cookie(COOKIE_SESSION_ID, "") | ||
sessionIdCookie.maxAge = 0 | ||
sessionIdCookie.path = COOKIE_PATH | ||
(RequestContextHolder.getRequestAttributes() as ServletRequestAttributes).response?.addCookie(sessionIdCookie) | ||
|
||
return ResponseEntity.ok().build() | ||
} | ||
} |