Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: saas example using Nuxt #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joshua-mo-143
Copy link
Member

@joshua-mo-143 joshua-mo-143 commented May 30, 2023

This version of the SaaS example which uses Nuxt has been kindly provided by @christ-offer, but unfortunately got PRed on the wrong repo. For reference: Nuxt is a front-end web framework that uses Vue as a base - similar to how Next.js uses React as a base.

Once this gets merged I'll delete the extra repo as it seems to be causing some confusion

Joshua Mo and others added 2 commits May 19, 2023 08:55
* Basic setup of project structure, the home/login/register pages now "work", css is still a bit janky

* basic setup of project structure, all files ported over, not tested fully yet, running into some weird db problems when using supabase

* added in some more fine-grained error logging to try and see what was going on with login

* some cleanup, mostly working now, not persistent, need to add pinia persist to store

* added in pinia persist to persist the login info

* removed non referenced folder from gitignore

* some cleanup, fixed the login function as it didnt do correct error handling on verify password
@christ-offer
Copy link

christ-offer commented Jun 13, 2023

Comment from me here, the nuxt template is not 100% ready for publishing yet, I have not implemented the modal functionality for single users or single deals, dayjob has taken precedence lately. (Silly me tried to implement it with slugs, which for obv reasons do not work with static SSR).

Also, did you remember to take the new login code from auth.rs and implement it in the Next template, the ok(true)/ok(false) part? (So that it actually checks the password correctly :) )

pub async fn login(
    State(state): State<AppState>,
    jar: PrivateCookieJar,
    Json(login): Json<LoginDetails>,
) -> Result<(PrivateCookieJar, StatusCode), StatusCode> {
    let query = sqlx::query("SELECT * FROM users WHERE email = $1")
        .bind(&login.email)
        .fetch_one(&state.postgres);

    match query.await {
        Ok(res) => {
            match bcrypt::verify(login.password, res.get("password")) {
                Ok(true) => {
                    let session_id = rand::random::<u64>().to_string();

                    sqlx::query("INSERT INTO sessions (session_id, user_id) VALUES ($1, $2) ON CONFLICT (user_id) DO UPDATE SET session_id = EXCLUDED.session_id")
                        .bind(&session_id)
                        .bind(res.get::<i32, _>("id"))
                        .execute(&state.postgres)
                        .await
                        .expect("Couldn't insert session :(");

                    let cookie = Cookie::build("foo", session_id)
                        .secure(true)
                        .same_site(SameSite::Strict)
                        .http_only(true)
                        .path("/")
                        .max_age(Duration::WEEK)
                        .finish();

                    Ok((jar.add(cookie), StatusCode::OK))
                }
                Ok(false) => Err(StatusCode::BAD_REQUEST),  // Password didn't match.
                Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),  // Something went wrong while verifying the password.
            }
        }
        Err(_) => Err(StatusCode::BAD_REQUEST),  // Something went wrong while fetching the user.
    }
}

@joshua-mo-143
Copy link
Member Author

joshua-mo-143 commented Oct 20, 2023

Hey @christ-offer, thanks again for submitting this and apologies it's taken so long to get this merged.

Is there any chance you could re-fork from this repo and then re-add the SaaS example? We'd love to have this as a community example. Unfortunately I accidentally created the extra shuttle-examples repo (see shuttle-examples-1) while doing something else, which means you can't actually edit anything yourself without a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants