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:migrateto apply the migration- Created an
index_controller.rb,indexaction of the controller, andviews/index/index.html.erb. Configuredroot('index#index')option inconfig/routes.rbto set theindexcontroller and actionindexas the default app's page - The login function will be implemented using sessions. Session vars
user_idanduser_namewill store user's id and name - Pass those session vars to the view via
@current_user_idand@current_user_namelocal variables.indexview displays whether user is logged in or not, and login/logout links depending on the current state. - Created
logoutaction 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.rbinitializer underconfig/initializersand will putclient_idandsecretthere. I will also includepublic_reposcope in order to have an access to user's public repos and implement the bonus task - Created an
index_helper.rbunderapp/helpersto put there all the miscellaneous functions - Created a
loginaction which will redirect user to GitHub's login page and add it toroutes.rb - Created a
callbackaction inindexcontroller. Mapped it to/oauthurlroutes.rbwhich is a callback url for GitHub OAuth app - Created a
github_access_tokenhelper inindexhelper. Function utilizenet/http, performs a POST request tohttps://github.com/login/oauth/access_tokenin order to receive theaccess_tokenfrom OAuth API - Created
get_user_detailsmethod inindexhelper 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_contentmethod inindexhelper which retrieves file from git repo - Created
insert_script_tagsmethod inindexhelper which checks theindex.htmlfile and insertsscripttags if required - Created
update_filemethod inindexhelper which performs a commit of an updated file