aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/auth/login.zig
blob: d7ee5cb7484c9533dd25e87a78494f6d9d3c43e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const std = @import("std");

const memora = @import("memora");
const Context = memora.Context;
const Storage = memora.Storage;

const Body = struct {
    user: []const u8,
    password: []const u8,
};

const Result = struct {
    success: bool,
};

pub const access = .everyone;

pub fn post(ctx: *Context, body: Body) anyerror!Result {
    var user = Storage.User.open(ctx.storage, body.user, ctx.allocator) catch return .{
        .success = false
    };
    defer user.deinit();

    const result = Result {
        .success = user.check_password(body.password)
    };

    if (result.success) {
        const session = try ctx.storage.sessions.add(ctx.storage, user.info);
        ctx.response.headers.fingerprint = session.fingerprint;
    }

    return result;
}