Class: Eksa::AuthController

Inherits:
Controller show all
Defined in:
lib/eksa/auth_controller.rb

Instance Attribute Summary

Attributes inherited from Controller

#flash, #redirect_url, #request, #status

Instance Method Summary collapse

Methods inherited from Controller

#asset_path, #current_user, #initialize, #javascript_tag, #params, #redirect_to, #render, #render_internal, #require_auth, #session, #stylesheet_tag

Constructor Details

This class inherits a constructor from Eksa::Controller

Instance Method Details

#loginObject



3
4
5
6
# File 'lib/eksa/auth_controller.rb', line 3

def 
  return redirect_to "/cms" if current_user
  render_internal 'auth/login'
end

#logoutObject



45
46
47
48
# File 'lib/eksa/auth_controller.rb', line 45

def logout
  session.delete('user_id')
  redirect_to "/", notice: "Anda telah berhasil logout."
end

#process_loginObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eksa/auth_controller.rb', line 14

def 
  username = params['username']
  password = params['password']
  
  user = Eksa::User.authenticate(username, password)
  if user
    session['user_id'] = user[:id]
    redirect_to "/cms", notice: "Selamat datang kembali, #{username}!"
  else
    redirect_to "/auth/login", notice: "Username atau password salah."
  end
end

#process_registerObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eksa/auth_controller.rb', line 27

def process_register
  if Eksa::User.all.any?
    return redirect_to "/auth/login", notice: "Registrasi ditutup. Hanya satu admin yang diizinkan."
  end

  username = params['username']
  password = params['password']
  
  if username && !username.empty? && password && password.length >= 6
    Eksa::User.create(username, password)
    user = Eksa::User.authenticate(username, password)
    session['user_id'] = user[:id]
    redirect_to "/cms", notice: "Akun berhasil dibuat. Selamat datang!"
  else
    redirect_to "/auth/register", notice: "Data tidak valid. Password minimal 6 karakter."
  end
end

#registerObject



8
9
10
11
12
# File 'lib/eksa/auth_controller.rb', line 8

def register
  return redirect_to "/cms" if current_user
  @admin_exists = Eksa::User.all.any?
  render_internal 'auth/register'
end