2020-12-24 16:24:04 +01:00
|
|
|
use {
|
2020-12-24 16:24:47 +01:00
|
|
|
crate::{db_create, db_find, db_update_with_conn},
|
|
|
|
diesel::prelude::*,
|
|
|
|
jirs_data::{Token, UserId},
|
2020-12-24 16:24:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
db_find! {
|
|
|
|
FindUserId,
|
|
|
|
msg => tokens => tokens.filter(user_id.eq(msg.user_id)).order_by(id.desc()),
|
|
|
|
Token,
|
|
|
|
user_id => UserId
|
|
|
|
}
|
|
|
|
|
|
|
|
db_find! {
|
|
|
|
FindBindToken,
|
|
|
|
msg => tokens => tokens.filter(bind_token.eq(Some(msg.token))),
|
|
|
|
Token,
|
|
|
|
token => uuid::Uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
db_update_with_conn! {
|
|
|
|
UseBindToken,
|
|
|
|
msg => conn => tokens => {
|
|
|
|
let token = FindBindToken { token: msg.token }.execute(conn)?;
|
|
|
|
diesel::update(tokens.find(token.id)).set(bind_token.eq(None as Option<uuid::Uuid>))
|
|
|
|
},
|
|
|
|
Token,
|
|
|
|
token => uuid::Uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
db_find! {
|
|
|
|
FindAccessToken,
|
|
|
|
msg => tokens => tokens.filter(access_token.eq(msg.token)),
|
|
|
|
Token,
|
|
|
|
token => uuid::Uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
db_create! {
|
|
|
|
CreateBindToken,
|
|
|
|
msg => tokens => diesel::insert_into(tokens).values((
|
|
|
|
user_id.eq(msg.user_id),
|
|
|
|
access_token.eq(uuid::Uuid::new_v4()),
|
|
|
|
refresh_token.eq(uuid::Uuid::new_v4()),
|
|
|
|
bind_token.eq(Some(uuid::Uuid::new_v4())),
|
|
|
|
)),
|
|
|
|
Token,
|
|
|
|
user_id => UserId
|
|
|
|
}
|