Sha256: 070c97708e4d5cbbea5ae7df005039df6fde0ed76548ac50c4b79426071c18e1

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

# Example for Authenticating a User

In `config/routes.rb`:
```rb
Rails.application.routes.draw do
  # ...... your other routes
  unless Rails.env.production?
    scope path: "/__cypress__", controller: 'cypress' do
      post "forceLogin", action: 'force_login'
    end
  end
end
```

`app/controllers/cypress_controller.rb`:
```rb
class CypressController < ApplicationController
  skip_before_action :verify_authenticity_token

  def force_login
    if params[:email].present?
      user = User.find_by!(email: params.require(:email))
    else
      user = User.first!
    end
    sign_in(user)
    redirect_to URI.parse(params.require(:redirect_to)).path
  end
end
```

In `cypress/support/on-rails.js`:
```js
Cypress.Commands.add('forceLogin', (details) => {
  if (!details) {
    details = {}
  }

  if (!details.redirect_to) {
    details.redirect_to = '/'
  }

  cy.visit('__cypress__/forceLogin',
    { method: 'POST', body: { email: details.email, redirect_to: details.redirect_to } })
})
```

Examples of usage in Cypress specs:
```js
cy.forceLogin()
cy.forceLogin({redirect_to: '/profile'})
cy.forceLogin({email: 'someuser@mail.com'})
```

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cypress-on-rails-1.17.0 docs/authentication.md
cypress-on-rails-1.16.0 docs/authentication.md
cypress-on-rails-1.15.1 docs/authentication.md
cypress-on-rails-1.15.0 docs/authentication.md
cypress-on-rails-1.14.0 docs/authentication.md