aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-28 07:50:35 +0000
committerAkshay <[email protected]>2020-12-28 07:50:35 +0000
commit3baea0e943ef89c7c73a5de51cbd41f78f8f0f14 (patch)
treeb1c7fe119b90f77daf170e10af000e0c1ed1270e /backend
parentd5aabbac71fcd12f31c82159fe0d7a58d0ad925d (diff)
add redis integration
Diffstat (limited to 'backend')
-rw-r--r--backend/Cargo.lock52
-rw-r--r--backend/Cargo.toml1
-rw-r--r--backend/src/handlers/users.rs12
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"
7checksum = "78d1833b3838dbe990df0f1f87baf640cf6146e898166afe401839d1b001e570" 7checksum = "78d1833b3838dbe990df0f1f87baf640cf6146e898166afe401839d1b001e570"
8dependencies = [ 8dependencies = [
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"
567checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" 567checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
568 568
569[[package]] 569[[package]]
570name = "bytes"
571version = "1.0.0"
572source = "registry+https://github.com/rust-lang/crates.io-index"
573checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72"
574
575[[package]]
570name = "bytestring" 576name = "bytestring"
571version = "0.1.5" 577version = "0.1.5"
572source = "registry+https://github.com/rust-lang/crates.io-index" 578source = "registry+https://github.com/rust-lang/crates.io-index"
573checksum = "fc7c05fa5172da78a62d9949d662d2ac89d4cc7355d7b49adee5163f1fb3f363" 579checksum = "fc7c05fa5172da78a62d9949d662d2ac89d4cc7355d7b49adee5163f1fb3f363"
574dependencies = [ 580dependencies = [
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]]
620name = "combine"
621version = "4.5.1"
622source = "registry+https://github.com/rust-lang/crates.io-index"
623checksum = "01199925a18b00193570e3d70cfe57dcb647eb167c29851983e76fc39e2fee39"
624dependencies = [
625 "bytes 1.0.0",
626 "memchr",
627]
628
629[[package]]
614name = "const_fn" 630name = "const_fn"
615version = "0.4.2" 631version = "0.4.2"
616source = "registry+https://github.com/rust-lang/crates.io-index" 632source = "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"
952source = "registry+https://github.com/rust-lang/crates.io-index" 969source = "registry+https://github.com/rust-lang/crates.io-index"
953checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" 970checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53"
954dependencies = [ 971dependencies = [
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"
1026source = "registry+https://github.com/rust-lang/crates.io-index" 1043source = "registry+https://github.com/rust-lang/crates.io-index"
1027checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" 1044checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9"
1028dependencies = [ 1045dependencies = [
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]]
1567name = "redis"
1568version = "0.19.0"
1569source = "registry+https://github.com/rust-lang/crates.io-index"
1570checksum = "1a6ddfecac9391fed21cce10e83c65fa4abafd77df05c98b1c647c65374ce9b3"
1571dependencies = [
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]]
1550name = "redox_syscall" 1582name = "redox_syscall"
1551version = "0.1.57" 1583version = "0.1.57"
1552source = "registry+https://github.com/rust-lang/crates.io-index" 1584source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1938,7 +1970,7 @@ version = "0.2.22"
1938source = "registry+https://github.com/rust-lang/crates.io-index" 1970source = "registry+https://github.com/rust-lang/crates.io-index"
1939checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" 1971checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd"
1940dependencies = [ 1972dependencies = [
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"
1958source = "registry+https://github.com/rust-lang/crates.io-index" 1990source = "registry+https://github.com/rust-lang/crates.io-index"
1959checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" 1991checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499"
1960dependencies = [ 1992dependencies = [
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"
18actix-cors = "0.5" 18actix-cors = "0.5"
19bigdecimal = "0.0.14" 19bigdecimal = "0.0.14"
20rand = "0.7" 20rand = "0.7"
21redis = "0.19"
21 22
22[dependencies.diesel] 23[dependencies.diesel]
23version = "1.4.2" 24version = "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};
9use bcrypt::{hash, verify, DEFAULT_COST}; 9use bcrypt::{hash, verify, DEFAULT_COST};
10use diesel::prelude::*; 10use diesel::prelude::*;
11use log::{error, info}; 11use log::{error, info};
12use redis::Commands;
12use serde::{Deserialize, Serialize}; 13use serde::{Deserialize, Serialize};
13 14
14pub async fn new_user( 15pub 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
87pub async fn logout(cookie: Identity) -> impl Responder { 96pub 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}