diff options
author | Akshay <[email protected]> | 2020-12-28 07:50:35 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-28 07:50:35 +0000 |
commit | 3baea0e943ef89c7c73a5de51cbd41f78f8f0f14 (patch) | |
tree | b1c7fe119b90f77daf170e10af000e0c1ed1270e /backend | |
parent | d5aabbac71fcd12f31c82159fe0d7a58d0ad925d (diff) |
add redis integration
Diffstat (limited to 'backend')
-rw-r--r-- | backend/Cargo.lock | 52 | ||||
-rw-r--r-- | backend/Cargo.toml | 1 | ||||
-rw-r--r-- | backend/src/handlers/users.rs | 12 |
3 files changed, 55 insertions, 10 deletions
diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 2f6aaaa..d309a66 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock | |||
@@ -7,7 +7,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
7 | checksum = "78d1833b3838dbe990df0f1f87baf640cf6146e898166afe401839d1b001e570" | 7 | checksum = "78d1833b3838dbe990df0f1f87baf640cf6146e898166afe401839d1b001e570" |
8 | dependencies = [ | 8 | dependencies = [ |
9 | "bitflags", | 9 | "bitflags", |
10 | "bytes", | 10 | "bytes 0.5.6", |
11 | "futures-core", | 11 | "futures-core", |
12 | "futures-sink", | 12 | "futures-sink", |
13 | "log", | 13 | "log", |
@@ -64,7 +64,7 @@ dependencies = [ | |||
64 | "base64", | 64 | "base64", |
65 | "bitflags", | 65 | "bitflags", |
66 | "brotli2", | 66 | "brotli2", |
67 | "bytes", | 67 | "bytes 0.5.6", |
68 | "cookie", | 68 | "cookie", |
69 | "copyless", | 69 | "copyless", |
70 | "derive_more", | 70 | "derive_more", |
@@ -229,7 +229,7 @@ dependencies = [ | |||
229 | "actix-rt", | 229 | "actix-rt", |
230 | "actix-service", | 230 | "actix-service", |
231 | "bitflags", | 231 | "bitflags", |
232 | "bytes", | 232 | "bytes 0.5.6", |
233 | "either", | 233 | "either", |
234 | "futures-channel", | 234 | "futures-channel", |
235 | "futures-sink", | 235 | "futures-sink", |
@@ -258,7 +258,7 @@ dependencies = [ | |||
258 | "actix-utils", | 258 | "actix-utils", |
259 | "actix-web-codegen", | 259 | "actix-web-codegen", |
260 | "awc", | 260 | "awc", |
261 | "bytes", | 261 | "bytes 0.5.6", |
262 | "derive_more", | 262 | "derive_more", |
263 | "encoding_rs", | 263 | "encoding_rs", |
264 | "futures-channel", | 264 | "futures-channel", |
@@ -412,7 +412,7 @@ dependencies = [ | |||
412 | "actix-rt", | 412 | "actix-rt", |
413 | "actix-service", | 413 | "actix-service", |
414 | "base64", | 414 | "base64", |
415 | "bytes", | 415 | "bytes 0.5.6", |
416 | "derive_more", | 416 | "derive_more", |
417 | "futures-core", | 417 | "futures-core", |
418 | "log", | 418 | "log", |
@@ -567,12 +567,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
567 | checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" | 567 | checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" |
568 | 568 | ||
569 | [[package]] | 569 | [[package]] |
570 | name = "bytes" | ||
571 | version = "1.0.0" | ||
572 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
573 | checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72" | ||
574 | |||
575 | [[package]] | ||
570 | name = "bytestring" | 576 | name = "bytestring" |
571 | version = "0.1.5" | 577 | version = "0.1.5" |
572 | source = "registry+https://github.com/rust-lang/crates.io-index" | 578 | source = "registry+https://github.com/rust-lang/crates.io-index" |
573 | checksum = "fc7c05fa5172da78a62d9949d662d2ac89d4cc7355d7b49adee5163f1fb3f363" | 579 | checksum = "fc7c05fa5172da78a62d9949d662d2ac89d4cc7355d7b49adee5163f1fb3f363" |
574 | dependencies = [ | 580 | dependencies = [ |
575 | "bytes", | 581 | "bytes 0.5.6", |
576 | ] | 582 | ] |
577 | 583 | ||
578 | [[package]] | 584 | [[package]] |
@@ -611,6 +617,16 @@ dependencies = [ | |||
611 | ] | 617 | ] |
612 | 618 | ||
613 | [[package]] | 619 | [[package]] |
620 | name = "combine" | ||
621 | version = "4.5.1" | ||
622 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
623 | checksum = "01199925a18b00193570e3d70cfe57dcb647eb167c29851983e76fc39e2fee39" | ||
624 | dependencies = [ | ||
625 | "bytes 1.0.0", | ||
626 | "memchr", | ||
627 | ] | ||
628 | |||
629 | [[package]] | ||
614 | name = "const_fn" | 630 | name = "const_fn" |
615 | version = "0.4.2" | 631 | version = "0.4.2" |
616 | source = "registry+https://github.com/rust-lang/crates.io-index" | 632 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -813,6 +829,7 @@ dependencies = [ | |||
813 | "log", | 829 | "log", |
814 | "pretty_env_logger", | 830 | "pretty_env_logger", |
815 | "rand", | 831 | "rand", |
832 | "redis", | ||
816 | "serde", | 833 | "serde", |
817 | "serde_derive", | 834 | "serde_derive", |
818 | "serde_json", | 835 | "serde_json", |
@@ -952,7 +969,7 @@ version = "0.2.6" | |||
952 | source = "registry+https://github.com/rust-lang/crates.io-index" | 969 | source = "registry+https://github.com/rust-lang/crates.io-index" |
953 | checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" | 970 | checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" |
954 | dependencies = [ | 971 | dependencies = [ |
955 | "bytes", | 972 | "bytes 0.5.6", |
956 | "fnv", | 973 | "fnv", |
957 | "futures-core", | 974 | "futures-core", |
958 | "futures-sink", | 975 | "futures-sink", |
@@ -1026,7 +1043,7 @@ version = "0.2.1" | |||
1026 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1043 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1027 | checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" | 1044 | checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" |
1028 | dependencies = [ | 1045 | dependencies = [ |
1029 | "bytes", | 1046 | "bytes 0.5.6", |
1030 | "fnv", | 1047 | "fnv", |
1031 | "itoa", | 1048 | "itoa", |
1032 | ] | 1049 | ] |
@@ -1547,6 +1564,21 @@ dependencies = [ | |||
1547 | ] | 1564 | ] |
1548 | 1565 | ||
1549 | [[package]] | 1566 | [[package]] |
1567 | name = "redis" | ||
1568 | version = "0.19.0" | ||
1569 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1570 | checksum = "1a6ddfecac9391fed21cce10e83c65fa4abafd77df05c98b1c647c65374ce9b3" | ||
1571 | dependencies = [ | ||
1572 | "async-trait", | ||
1573 | "combine", | ||
1574 | "dtoa", | ||
1575 | "itoa", | ||
1576 | "percent-encoding 2.1.0", | ||
1577 | "sha1", | ||
1578 | "url 2.1.1", | ||
1579 | ] | ||
1580 | |||
1581 | [[package]] | ||
1550 | name = "redox_syscall" | 1582 | name = "redox_syscall" |
1551 | version = "0.1.57" | 1583 | version = "0.1.57" |
1552 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1584 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1938,7 +1970,7 @@ version = "0.2.22" | |||
1938 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1970 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1939 | checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" | 1971 | checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" |
1940 | dependencies = [ | 1972 | dependencies = [ |
1941 | "bytes", | 1973 | "bytes 0.5.6", |
1942 | "futures-core", | 1974 | "futures-core", |
1943 | "iovec", | 1975 | "iovec", |
1944 | "lazy_static", | 1976 | "lazy_static", |
@@ -1958,7 +1990,7 @@ version = "0.3.1" | |||
1958 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1990 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1959 | checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" | 1991 | checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" |
1960 | dependencies = [ | 1992 | dependencies = [ |
1961 | "bytes", | 1993 | "bytes 0.5.6", |
1962 | "futures-core", | 1994 | "futures-core", |
1963 | "futures-sink", | 1995 | "futures-sink", |
1964 | "log", | 1996 | "log", |
diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 8ce728b..fad11b0 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml | |||
@@ -18,6 +18,7 @@ actix-identity = "0.3.1" | |||
18 | actix-cors = "0.5" | 18 | actix-cors = "0.5" |
19 | bigdecimal = "0.0.14" | 19 | bigdecimal = "0.0.14" |
20 | rand = "0.7" | 20 | rand = "0.7" |
21 | redis = "0.19" | ||
21 | 22 | ||
22 | [dependencies.diesel] | 23 | [dependencies.diesel] |
23 | version = "1.4.2" | 24 | version = "1.4.2" |
diff --git a/backend/src/handlers/users.rs b/backend/src/handlers/users.rs index 8423384..775635e 100644 --- a/backend/src/handlers/users.rs +++ b/backend/src/handlers/users.rs | |||
@@ -9,6 +9,7 @@ use actix_web::{web, HttpResponse, Responder}; | |||
9 | use bcrypt::{hash, verify, DEFAULT_COST}; | 9 | use bcrypt::{hash, verify, DEFAULT_COST}; |
10 | use diesel::prelude::*; | 10 | use diesel::prelude::*; |
11 | use log::{error, info}; | 11 | use log::{error, info}; |
12 | use redis::Commands; | ||
12 | use serde::{Deserialize, Serialize}; | 13 | use serde::{Deserialize, Serialize}; |
13 | 14 | ||
14 | pub async fn new_user( | 15 | pub async fn new_user( |
@@ -74,6 +75,14 @@ pub async fn login( | |||
74 | let hashed_pass = selected_user.password; | 75 | let hashed_pass = selected_user.password; |
75 | if verify(entered_pass, &hashed_pass).unwrap() { | 76 | if verify(entered_pass, &hashed_pass).unwrap() { |
76 | cookie.remember(login_details.username.clone()); | 77 | cookie.remember(login_details.username.clone()); |
78 | let redis_client = redis::Client::open("redis://127.0.0.1/").unwrap(); | ||
79 | let mut redis_conn = redis_client.get_connection().unwrap(); | ||
80 | redis_conn | ||
81 | .set::<String, String, String>( | ||
82 | login_details.username.clone(), | ||
83 | cookie.identity().unwrap(), | ||
84 | ) | ||
85 | .unwrap(); | ||
77 | info!( | 86 | info!( |
78 | "Successful login: {} {}", | 87 | "Successful login: {} {}", |
79 | selected_user.username, selected_user.email_id | 88 | selected_user.username, selected_user.email_id |
@@ -85,6 +94,9 @@ pub async fn login( | |||
85 | } | 94 | } |
86 | 95 | ||
87 | pub async fn logout(cookie: Identity) -> impl Responder { | 96 | pub async fn logout(cookie: Identity) -> impl Responder { |
97 | let redis_client = redis::Client::open("redis://127.0.0.1/").unwrap(); | ||
98 | let mut redis_conn = redis_client.get_connection().unwrap(); | ||
99 | redis_conn.del::<String, String>(cookie.identity().unwrap()); | ||
88 | cookie.forget(); | 100 | cookie.forget(); |
89 | HttpResponse::Ok().body("Successful logout.") | 101 | HttpResponse::Ok().body("Successful logout.") |
90 | } | 102 | } |