Skip to content

Commit

Permalink
implement /logout endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jan 29, 2022
1 parent 91275a4 commit af4bc02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cookies.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ pub fn parse(alloc: std.mem.Allocator, headers: http.Request.Headers) !Jar {
}
return map;
}

pub fn delete(response: *http.Response, comptime name: string) !void {
try response.headers.put("Set-Cookie", name ++ "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT");
}
11 changes: 11 additions & 0 deletions src/handler/_handler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const json = @import("json");

const mime = @import("../mime.zig");
const db = @import("../db/_db.zig");
const cookies = @import("../cookies.zig");

const _internal = @import("./_internal.zig");
const _index = @import("./index.zig");
Expand All @@ -31,6 +32,7 @@ pub fn getHandler(comptime oa2: type) http.RequestHandler(void) {
return http.router.Router(void, &.{
http.router.get("/", Middleware(_index.get).next),
file_route("/theme.css"),
http.router.get("/logout", Middleware(logout).next),
http.router.get("/about", Middleware(StaticPek("/about.pek", "About").get).next),
http.router.get("/contact", Middleware(StaticPek("/contact.pek", "Contact").get).next),
http.router.get("/login", Middleware(oa2.login).next),
Expand Down Expand Up @@ -118,3 +120,12 @@ pub fn saveInfo(response: *http.Response, request: http.Request, idp: oauth2.Pro
pub fn getAccessToken(ulid: string) ?string {
return _internal.access_tokens.get(ulid);
}

pub fn logout(_: void, response: *http.Response, request: http.Request, args: struct {}) !void {
_ = args;
_ = response;
_ = request;

try cookies.delete(response, "jwt");
try _internal.redirectTo(response, "./");
}

0 comments on commit af4bc02

Please sign in to comment.