summaryrefslogtreecommitdiff
path: root/src/lib/auth/components/LoginMobile.jsx
blob: 095f41747eee9171e0e518d4e0c96410ac6d9233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import Image from 'next/image'
import IndoteknikLogo from '@/images/logo.png'
import Link from '@/core/components/elements/Link/Link'
import Alert from '@/core/components/elements/Alert/Alert'
import MobileView from '@/core/components/views/MobileView'
import useLogin from '../hooks/useLogin'

import { useSession, signIn, SignOut } from 'next-auth/react'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { setCookie } from 'cookies-next'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner'
import odooApi from '@/core/api/odooApi'
import { getAuth } from '@/core/utils/auth'

const LoginMobile = () => {
  const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef, handleGoogleSubmit } =
    useLogin()

  const router = useRouter()
  const [query, setQuery] = useState(router?.query?.next || '/')
  const { data: session } = useSession()
  const auth = getAuth()

  const handleGoogle = async () => {
    const url = query != '/' ? '/login?source=google&next=' + query : '/login?source=google'
    await signIn('google', { callbackUrl: url })
  }
  useEffect(() => {
    if (session) {
      handleGoogleSubmit(session)
    }
  }, [session])

  if (router.query.source) {
    return (
      <BottomPopup active={true} close={true}>
        <div className='leading-7 text-gray_r-12/80 flex justify-center'>Mohon Tunggu</div>
        <div className='container flex justify-center my-4'>
          <LogoSpinner width={48} height={48} />
        </div>
      </BottomPopup>
    )
  }

  return (
    <MobileView>
      <div className='p-6 pt-10 flex flex-col items-center min-h-screen'>
        <Link href='/'>
          <Image src={IndoteknikLogo} alt='Logo Indoteknik' width={150} height={50} />
        </Link>
        <h1 className='text-2xl mt-4 font-semibold'>Mulai Belanja Sekarang</h1>
        <h2 className='text-gray_r-11 font-normal mt-1 mb-4'>Masuk ke akun kamu untuk belanja</h2>

        {alert && (
          <Alert className='text-center' type={alert.type}>
            {alert.children}
          </Alert>
        )}

        <form className='w-full mt-6 flex flex-col gap-y-4' onSubmit={handleSubmit}>
          <div>
            <label htmlFor='email'>Alamat Email</label>
            <input
              type='email'
              name='email'
              id='email'
              className='form-input w-full mt-3'
              ref={emailRef}
              onChange={handleChangeInput}
              placeholder='contoh@email.com'
            />
          </div>
          <div>
            <div className='flex justify-between'>
              <label htmlFor='password'>Kata Sandi</label>
              <Link href='/forgot-password'>Lupa Kata Sandi</Link>
            </div>
            <input
              type='password'
              name='password'
              id='password'
              className='form-input w-full mt-3'
              ref={passwordRef}
              onChange={handleChangeInput}
              placeholder='••••••••••••'
            />
          </div>
          <button type='submit' className='btn-yellow w-full mt-2' disabled={!isValid || isLoading}>
            {!isLoading ? 'Masuk' : 'Loading...'}
          </button>
        </form>
        <div className='flex  items-center mt-3 mb-3'>
          <hr className='flex-1' />
          <p className='text-gray-400'>ATAU</p>
          <hr className='flex-1' />
        </div>

        <button
          type='submit'
          className='border border-gray-500 p-2 rounded-md hover:bg-gray-100  w-full mt-2 flex items-center justify-center gap-x-2'
          onClick={() => handleGoogle()}
        >
          <Image
            src='/images/icons8-google.svg'
            alt='google image'
            className='h-7 w-7'
            width={10}
            height={10}
          />
          <p>Masuk dengan Google</p>
        </button>

        <div className='text-gray_r-11 mt-4'>
          Belum punya akun Indoteknik?{' '}
          <Link href='/register' className='inline'>
            Daftar
          </Link>
        </div>
      </div>
    </MobileView>
  )
}

export default LoginMobile