This app enables a user to login using GitHub OAuth API and save the user details once the login was successful. It also ensures that the data of the particular user was not saved before.
The app inserts the script
tags into head
and body
sections of a powr/index.html
in evil1/evil1.github.io
repository.
There is also a dummy validation that script
tags where not inserted before to avoid duplication.
- First I've generated the basic scaffold for the user entity
rails generate scaffold user external_id:string login:string name:string jsonObject:text
. That step has created a database migration, generated a model, and controller with a basic CRUD functionality rails db:migrate
to apply the migration- Created an
index_controller.rb
,index
action of the controller, andviews/index/index.html.erb
. Configuredroot('index#index')
option inconfig/routes.rb
to set theindex
controller and actionindex
as the default app's page - The login function will be implemented using sessions. Session vars
user_id
anduser_name
will store user's id and name - Pass those session vars to the view via
@current_user_id
and@current_user_name
local variables.index
view displays whether user is logged in or not, and login/logout links depending on the current state. - Created
logout
action which simply resets the session and redirects to/
- According to the GitHub's docs in order to login using OAuth we need to send GET request to
https://github.com/login/oauth/authorize
. The only required parameter is thecliend_id
. For the purpose of storing GitHub API credintials I will create angithub.rb
initializer underconfig/initializers
and will putclient_id
andsecret
there. I will also includepublic_repo
scope in order to have an access to user's public repos and implement the bonus task - Created an
index_helper.rb
underapp/helpers
to put there all the miscellaneous functions - Created a
login
action which will redirect user to GitHub's login page and add it toroutes.rb
- Created a
callback
action inindex
controller. Mapped it to/oauth
urlroutes.rb
which is a callback url for GitHub OAuth app - Created a
github_access_token
helper inindex
helper. Function utilizenet/http
, performs a POST request tohttps://github.com/login/oauth/access_token
in order to receive theaccess_token
from OAuth API - Created
get_user_details
method inindex
helper which retrieves user's details - Implemented the details saving functionality. This is the end of the task itself. To ensure that the details
where save correctly we can simply access the
http://loclhost:3000/users
- Created
get_file_content
method inindex
helper which retrieves file from git repo - Created
insert_script_tags
method inindex
helper which checks theindex.html
file and insertsscript
tags if required - Created
update_file
method inindex
helper which performs a commit of an updated file