-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redirect to referrer page after login (#249)
* feat: redirect to referer page after login * better * fmt * central redirect functions in a file * fmt * add license * direct define redirecturl in callback fn * fmt * merge `https.ts` into `redirect.ts` along with test * Update utils/redirect.ts --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
- Loading branch information
Showing
12 changed files
with
151 additions
and
57 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
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import type { RedirectStatus, Status } from "std/http/http_status.ts"; | ||
import { deleteCookie, getCookies, setCookie } from "std/http/cookie.ts"; | ||
|
||
export const REDIRECT_URL_COOKIE_NAME = "redirect-url"; | ||
|
||
/** | ||
* @param location A relative (to the request URL) or absolute URL. | ||
* @param status HTTP status | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location} | ||
*/ | ||
export function redirect( | ||
location: string, | ||
status: Status.Created | RedirectStatus = 303, | ||
) { | ||
return new Response(null, { | ||
headers: { | ||
location, | ||
}, | ||
status, | ||
}); | ||
} | ||
|
||
export function redirectToLogin(url: string) { | ||
return redirect(`/signin?from=${url}`); | ||
} | ||
|
||
export function setRedirectUrlCookie(req: Request, res: Response) { | ||
const from = new URL(req.url).searchParams.get("from"); | ||
setCookie(res.headers, { | ||
name: REDIRECT_URL_COOKIE_NAME, | ||
value: from ?? req.headers.get("referer")!, | ||
path: "/", | ||
}); | ||
} | ||
|
||
export function deleteRedirectUrlCookie(headers: Headers) { | ||
deleteCookie(headers, REDIRECT_URL_COOKIE_NAME); | ||
} | ||
|
||
export function getRedirectUrlCookie(headers: Headers) { | ||
return getCookies(headers)[REDIRECT_URL_COOKIE_NAME]; | ||
} |
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,78 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { | ||
deleteRedirectUrlCookie, | ||
getRedirectUrlCookie, | ||
redirect, | ||
REDIRECT_URL_COOKIE_NAME, | ||
redirectToLogin, | ||
setRedirectUrlCookie, | ||
} from "./redirect.ts"; | ||
import { assert, assertEquals } from "std/testing/asserts.ts"; | ||
|
||
Deno.test("[redirect] redirect() defaults", () => { | ||
const location = "/hello-there"; | ||
|
||
const response = redirect(location); | ||
assert(!response.ok); | ||
assertEquals(response.body, null); | ||
assertEquals(response.headers.get("location"), location); | ||
assertEquals(response.status, 303); | ||
}); | ||
|
||
Deno.test("[redirect] redirect()", () => { | ||
const location = "/hello-there"; | ||
const status = 302; | ||
|
||
const response = redirect(location, status); | ||
assert(!response.ok); | ||
assertEquals(response.body, null); | ||
assertEquals(response.headers.get("location"), location); | ||
assertEquals(response.status, status); | ||
}); | ||
|
||
Deno.test("[redirect] redirectToLogin()", () => { | ||
const from = "/hello-there"; | ||
const response = redirectToLogin(from); | ||
const location = `/signin?from=${from}`; | ||
assert(!response.ok); | ||
assertEquals(response.body, null); | ||
assertEquals(response.headers.get("location"), location); | ||
assertEquals(response.status, 303); | ||
}); | ||
|
||
Deno.test("[redirect] setRedirectUrlCookie()", () => { | ||
const redirectUrl = "/hello-there"; | ||
const request = new Request(`http://example.com/signin?from=${redirectUrl}`); | ||
const response = new Response(); | ||
setRedirectUrlCookie(request, response); | ||
const cookieHeader = response.headers.get("set-cookie"); | ||
assertEquals( | ||
cookieHeader, | ||
`${REDIRECT_URL_COOKIE_NAME}=${redirectUrl}; Path=/`, | ||
); | ||
}); | ||
|
||
Deno.test("[redirect] getRedirectUrlCookie()", () => { | ||
const headers = new Headers(); | ||
const redirectUrl = "/hello-there"; | ||
headers.set("Cookie", `${REDIRECT_URL_COOKIE_NAME}=${redirectUrl}`); | ||
assertEquals(getRedirectUrlCookie(headers), redirectUrl); | ||
}); | ||
|
||
Deno.test("[redirect] deleteRedirectUrlCookie()", () => { | ||
const redirectUrl = "/hello-there"; | ||
const request = new Request(`http://example.com/signin?from=${redirectUrl}`); | ||
const response = new Response(); | ||
setRedirectUrlCookie(request, response); | ||
assert( | ||
!response.headers.get("set-cookie")?.includes( | ||
`${REDIRECT_URL_COOKIE_NAME}=;`, | ||
), | ||
); | ||
deleteRedirectUrlCookie(response.headers); | ||
assert( | ||
response.headers.get("set-cookie")?.includes( | ||
`${REDIRECT_URL_COOKIE_NAME}=;`, | ||
), | ||
); | ||
}); |