-
Notifications
You must be signed in to change notification settings - Fork 0
Add Google OAuth sign and and user attributes #112
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
Changes from all commits
0d2fa1b
a5fe28b
245710d
3f2cb72
670d2d8
ce35ed1
ea8b334
bb52dff
5d464eb
037d585
733e801
54d6502
8528ef9
9c36ed8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,72 @@ | ||
| class SessionsController < ApplicationController | ||
| allow_unauthenticated_access only: [ :new, :create ] | ||
| allow_unauthenticated_access only: [ :new, :create, :failure ] | ||
| rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to root_url, alert: "Try again later." } | ||
|
|
||
| def new | ||
| render :new | ||
| end | ||
|
|
||
| def create | ||
| # Rails.logger.debug "Strava Client ID: #{ENV['STRAVA_CLIENT_ID'].inspect}" | ||
| # Rails.logger.debug "Strava Client Secret: #{ENV['STRAVA_CLIENT_SECRET'].inspect}" | ||
| auth = request.env["omniauth.auth"] | ||
| user = User.find_or_create_from_strava(auth) | ||
|
|
||
| if user.persisted? | ||
| session = Session.create!( | ||
| user: user, | ||
| ip_address: request.remote_ip, | ||
| user_agent: request.user_agent | ||
| ) | ||
|
|
||
| cookies.signed[:session_id] = { value: session.id, httponly: true } | ||
| redirect_to root_path, notice: "Successfully signed in with Strava!" | ||
| if auth.blank? | ||
| redirect_to new_session_path, alert: "Authentication failed. Please try again." | ||
| return | ||
| end | ||
|
|
||
| if linking_strava?(auth) | ||
| link_strava_account(auth) | ||
| else | ||
| redirect_to root_path, alert: "Failed to sign in with Strava." | ||
| sign_in_user(auth) | ||
| end | ||
|
apdarr marked this conversation as resolved.
|
||
| end | ||
|
|
||
| def failure | ||
| redirect_to new_session_path, alert: "Authentication failed: #{params[:message]&.humanize}." | ||
| end | ||
|
|
||
| def destroy | ||
| terminate_session | ||
| redirect_to root_path, notice: "Successfully signed out!" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def linking_strava?(auth) | ||
| request.env["omniauth.origin"] == "link_strava" && authenticated? && auth.provider == "strava" | ||
| end | ||
|
|
||
| def link_strava_account(auth) | ||
| existing_user = User.find_by(strava_id: auth.uid) | ||
|
|
||
| if existing_user && existing_user != Current.user | ||
| redirect_to edit_profile_path, alert: "This Strava account is already linked to another user." | ||
| return | ||
| end | ||
|
|
||
| Current.user.link_strava!(auth) | ||
| redirect_to edit_profile_path, notice: "Strava account connected successfully!" | ||
| end | ||
|
|
||
| def sign_in_user(auth) | ||
| user = find_or_create_user(auth) | ||
|
|
||
| if user.persisted? | ||
| start_new_session_for(user) | ||
| redirect_to root_path, notice: "Successfully signed in!" | ||
| else | ||
| redirect_to root_path, alert: "Failed to sign in." | ||
| end | ||
| end | ||
|
|
||
| def find_or_create_user(auth) | ||
| case auth.provider | ||
| when "strava" | ||
| User.find_or_create_from_strava(auth) | ||
| when "google_oauth2" | ||
| User.find_or_create_from_google(auth) | ||
| else | ||
| raise "Unsupported OAuth provider: #{auth.provider}" | ||
| end | ||
| end | ||
| end | ||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.