Skip to content

Commit

Permalink
build: create user and session
Browse files Browse the repository at this point in the history
  • Loading branch information
tuoanhnt95 committed Nov 26, 2023
1 parent 4ee485e commit 241094b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 13 deletions.
3 changes: 2 additions & 1 deletion photo-review-api/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ApplicationController < ActionController::API
include ActionController::RequestForgeryProtection

protect_from_forgery with: :exception, unless: -> { request.format.json? }
# protect_from_forgery with: :exception, unless: -> { request.format.json? }
protect_from_forgery with: :null_session, unless: -> { request.format.json? }

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

solve error: Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity

https://qiita.com/nishina555/items/4ffaf5cc57a384b66230

before_action :authenticate_user!
end
68 changes: 68 additions & 0 deletions photo-review-api/app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]

# GET /resource/sign_up
# def new
# super
# end

# POST /resource
# def create
# p '------------------'

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

Todo: delete the 'p' debug codes

# p '------------------'
# p 'create user'
# p params[:email]
# p '------------------'
# p '------------------'
# super
# end

# GET /resource/edit
# def edit
# super
# end

# PUT /resource
def update
super
end

# DELETE /resource
# def destroy
# super
# end

# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end

# protected

# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end

# If you have extra params to permit, append them to the sanitizer.
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:username])
end

# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end

# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
7 changes: 4 additions & 3 deletions photo-review-api/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.use ActionDispatch::Flash

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

Fix error: undefined method 'flash' for #< ActionDispatch::Request:0x007f99f41d8720

https://stackoverflow.com/a/19601252/19858571


# disable session cookies temporarily to bypass errors for testing
# will need to re-enable later
# error: when uploading photo to album, getting error:
# https://github.com/waiting-for-dev/devise-jwt/issues/235
# config.session_store :cookie_store, key: '_interslice_session'
# config.middleware.use ActionDispatch::Cookies
# config.middleware.use config.session_store, config.session_options
config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies
config.middleware.use config.session_store, config.session_options

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

Fix error: ActionDispatch::Request::Session::DisabledSessionError: Your application has sessions disabled. To write to the session you must first configure a session store

waiting-for-dev/devise-jwt#235 (comment)

end
end
3 changes: 2 additions & 1 deletion photo-review-api/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@
# should add them to the navigational formats lists.
#
# The "*/*" below is required to match Internet Explorer requests.
config.navigational_formats = ['*/*', :html, :turbo_stream]
# config.navigational_formats = ['*/*', :html, :turbo_stream]
config.navigational_formats = []

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

Fix error: NameError (undefined local variable or method `flash' for #Users::RegistrationsController:0x0000000000bfb8

  flash[key] = message if message.present?

heartcombo/devise#4275 (comment)

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
Expand Down
6 changes: 5 additions & 1 deletion photo-review-api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
put '/photos/:photo_id/photo_user_reviews', to: 'photo_user_reviews#update', as: 'update_review'
delete '/delete_photos', to: 'photos#destroy_multiple', as: 'delete_photos'
get '/albums/:album_id/upload_progress', to: 'uploads#show_progress', as: 'upload_progress'
devise_for :users
devise_for :users, controllers: {
registrations: 'users/registrations'

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

To add custom field "username", must create a custom controller for user registration. The controller in Devise only configures for two fields "email" and "password".

https://github.com/heartcombo/devise
Configuring controllers

} do
post '/users', to: 'users/registrations#create'
end
end

# TODO: Add a route to get all reviews for a photo
35 changes: 28 additions & 7 deletions photo-review-client/src/views/Signup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,49 @@
<h1>Signup</h1>
<form @submit="handleSubmit">
<label for="email">Email:</label>
<input type="email" id="email" v-model="email" required>
<input type="email" id="email" v-model="email" required class="text-black border">

This comment has been minimized.

Copy link
@tuoanhnt95

tuoanhnt95 Nov 26, 2023

Author Owner

change text color in text box from white to black to see the text

<label for="password">Password:</label>
<input type="password" id="password" v-model="password" required>
<input type="password" id="password" v-model="password" required class="text-black border">
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" v-model="password2" required>
<input type="password" id="confirmPassword" v-model="password2" required class="text-black border">
<button type="submit">Sign Up</button>
</form>
</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
const email = ref('');
const password = ref('');
const password2 = ref('');
const handleSubmit = (event: Event) => {
const handleSubmit = async (event: Event) => {
event.preventDefault();
// Perform signup logic here
console.log('Email:', email.value);
console.log('Password:', password.value);
if (password.value !== password2.value) {
alert('Passwords do not match');
return;
}
try {
const response = await axios.post('http://localhost:3000/users', {
user: {
email: email.value,
password: password.value
}
// email: email.value,
// password: password.value
});
console.log('Registration successful:', response.data);
// Handle success response here
// redirect to login page
} catch (error) {
console.error('Registration failed:', error);
// Handle error response here
}
};
</script>

Expand Down

0 comments on commit 241094b

Please sign in to comment.