Skip to content

Commit

Permalink
Merge pull request #3 from victorhqc/feat/dynamic_redirect_url
Browse files Browse the repository at this point in the history
feat: optional redirect to client
  • Loading branch information
victorhqc authored Feb 26, 2020
2 parents 764047d + 9024089 commit c3a8c99
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 2 additions & 0 deletions julie-vic-wedding-api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ TOKEN_SECRET=xxx
RSVP_LIMIT_DATE=2020-03-01

PUBLIC_API_URL=http://localhost:7878
# Optional, If set. API will redirect to that URL after user creation.
REDIRECT_CLIENT_URL=http://localhost:300/
2 changes: 1 addition & 1 deletion julie-vic-wedding-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "julie-vic-wedding-api"
version = "0.1.0"
version = "0.2.0"
authors = ["Victor Quiroz Castro <victorhqc@gmail.com>"]
edition = "2018"

Expand Down
14 changes: 11 additions & 3 deletions julie-vic-wedding-api/src/handlers/auth/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gotham::helpers::http::response::{
use gotham::state::{FromState, State};
use hyper::{Body, Response, StatusCode};
use julie_vic_wedding_core::models::User;
use std::env;

use crate::auth::encode_token;
use crate::auth::google::{
Expand Down Expand Up @@ -39,10 +40,17 @@ pub fn google_redirect_handler(mut state: State) -> Box<HandlerFuture> {
let results = find_or_create(repo, profile).then(|result| match result {
Ok(user) => {
let token = encode_token(&user, 3600);
let response = AuthenticatedUser { user, token };
let redirect_url = env::var("REDIRECT_CLIENT_URL");

let res = match redirect_url {
Ok(u) => create_temporary_redirect(&state, format!("{}/token?token={}", u, token)),
_ => {
let response = AuthenticatedUser { user, token };
let body = serde_json::to_string(&response).expect("Failed to serialize user.");
create_response(&state, StatusCode::OK, mime::APPLICATION_JSON, body)
}
};

let body = serde_json::to_string(&response).expect("Failed to serialize user.");
let res = create_response(&state, StatusCode::OK, mime::APPLICATION_JSON, body);
future::ok((state, res))
}
Err(_e) => {
Expand Down
13 changes: 1 addition & 12 deletions julie-vic-wedding-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "julie-vic-wedding-core"
version = "0.1.0"
version = "0.2.0"
authors = ["Victor Quiroz Castro <victorhqc@gmail.com>"]
edition = "2018"

Expand All @@ -13,23 +13,12 @@ diesel = { version = "1.4", features = ["postgres", "r2d2", "chrono", "extras"]
dotenv = "0.15"
env_logger = "0.6.0"
futures = "0.1"
# gotham = "0.4"
# gotham_derive = "0.4"
# gotham_middleware_diesel = "0.1.0"
# gotham_middleware_jwt = "0.3.3"
http = "0.1"
hyper = "0.12"
# jsonwebtoken = "6.0"
log = "0.4"
# mime = "0.3"
# oauth2 = "2.0"
r2d2 = "0.8"
# reqwest = { version = "0.9" }
# tokio = "0.1.17"
# tokio-threadpool = "0.1.12"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
# url = "1.7"
uuid = { version = "0.6", features = ["serde", "v4"] }
openssl = "*"

0 comments on commit c3a8c99

Please sign in to comment.