DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: a8f056d1523e00e69662e2868f3976103ead82ff deck-builder/sdbclient/src/api/user/mod.rs -rw-r--r-- 1.7 KiB
a8f056d1Jonni Liljamo Move register/login to plugins, implement mostly 2 years ago
                                                                                
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * This file is part of sdbclient
 * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use reqwest;
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
pub struct PostToken {
    pub email: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResultToken {
    pub token: String,
    pub id: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ResponseToken {
    Error { error: String },
    Valid(ResultToken),
}

pub fn token(api_address: String, email: String, password: String) -> ResponseToken {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .post(&format!("{}/user/token", api_address))
        .json(&PostToken { email, password })
        .send()
        .unwrap();

    resp.json().unwrap()
}

#[derive(Serialize)]
pub struct PostRegister {
    pub username: String,
    pub email: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResultRegister {
    pub id: String,
    pub username: String,
    pub email: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ResponseRegister {
    Error { error: String },
    Valid(ResultRegister),
}

pub fn register(
    api_address: String,
    username: String,
    email: String,
    password: String,
) -> ResponseRegister {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .post(&format!("{}/user/register", api_address))
        .json(&PostRegister {
            username,
            email,
            password,
        })
        .send()
        .unwrap();

    resp.json().unwrap()
}