login.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <template lang="pug">
  2. v-app
  3. .login
  4. .login-sd
  5. .d-flex
  6. .login-logo
  7. v-avatar(tile, size='34')
  8. v-img(:src='logoUrl')
  9. .login-title
  10. .text-h6 {{ siteTitle }}
  11. //-------------------------------------------------
  12. //- PROVIDERS LIST
  13. //-------------------------------------------------
  14. template(v-if='screen === `login` && strategies.length > 1')
  15. .login-subtitle.mt-5
  16. .text-subtitle-1 Select Authentication Provider
  17. .login-list
  18. v-list.elevation-1.radius-7(nav)
  19. v-list-item-group(v-model='selectedStrategyKey')
  20. v-list-item(
  21. v-for='(stg, idx) of strategies'
  22. :key='stg.key'
  23. :value='stg.key'
  24. :color='stg.strategy.color'
  25. )
  26. v-avatar.mr-3(tile, size='24', v-html='stg.strategy.icon')
  27. span.text-none {{stg.displayName}}
  28. //-------------------------------------------------
  29. //- LOGIN FORM
  30. //-------------------------------------------------
  31. template(v-if='screen === `login` && selectedStrategy.strategy.useForm')
  32. .login-subtitle
  33. .text-subtitle-1 Enter your credentials
  34. .login-form
  35. v-text-field(
  36. solo
  37. flat
  38. prepend-inner-icon='mdi-clipboard-account'
  39. background-color='white'
  40. hide-details
  41. ref='iptEmail'
  42. v-model='username'
  43. :placeholder='$t("auth:fields.emailUser")'
  44. )
  45. v-text-field.mt-2(
  46. solo
  47. flat
  48. prepend-inner-icon='mdi-form-textbox-password'
  49. background-color='white'
  50. hide-details
  51. ref='iptPassword'
  52. v-model='password'
  53. :append-icon='hidePassword ? "mdi-eye-off" : "mdi-eye"'
  54. @click:append='() => (hidePassword = !hidePassword)'
  55. :type='hidePassword ? "password" : "text"'
  56. :placeholder='$t("auth:fields.password")'
  57. @keyup.enter='login'
  58. )
  59. v-btn.mt-2.text-none(
  60. width='100%'
  61. v-if='screen === "login"'
  62. large
  63. color='primary'
  64. dark
  65. @click='login'
  66. :loading='isLoading'
  67. ) {{ $t('auth:actions.login') }}
  68. .text-center.mt-5(v-if='screen === "login"')
  69. v-btn.text-none(
  70. text
  71. rounded
  72. color='grey darken-3'
  73. @click.stop.prevent='forgotPassword'
  74. href='#forgot'
  75. ): .caption {{ $t('auth:forgotPasswordLink') }}
  76. v-btn.text-none(
  77. v-if='screen === "login" && selectedStrategyKey === `local` && selectedStrategy.selfRegistration'
  78. color='indigo darken-2'
  79. text
  80. rounded
  81. href='/register'
  82. ): .caption {{ $t('auth:switchToRegister.link') }}
  83. //- .login-main
  84. //- v-container(grid-list-lg, fluid)
  85. //- v-row(no-gutters)
  86. //- v-col(cols='12', xl='4')
  87. //- transition(name='fadeUp')
  88. //- v-card.elevation-5(v-show='isShown', light)
  89. //- v-toolbar(color='indigo', flat, dense, dark)
  90. //- v-spacer
  91. //- .subheading(v-if='screen === "tfa"') {{ $t('auth:tfa.subtitle') }}
  92. //- .subheading(v-if='screen === "changePwd"') {{ $t('auth:changePwd.subtitle') }}
  93. //- .subheading(v-else-if='selectedStrategy.key !== "local"') {{ $t('auth:loginUsingStrategy', { strategy: selectedStrategy.title, interpolation: { escapeValue: false } }) }}
  94. //- .subheading(v-else) {{ $t('auth:loginRequired') }}
  95. //- v-spacer
  96. //- v-card-text.text-center
  97. //- h1.display-1.indigo--text.py-2 {{ siteTitle }}
  98. //- template(v-if='screen === "login"')
  99. //- v-text-field.mt-3(
  100. //- solo
  101. //- flat
  102. //- prepend-icon='mdi-clipboard-account'
  103. //- background-color='grey lighten-4'
  104. //- hide-details
  105. //- ref='iptEmail'
  106. //- v-model='username'
  107. //- :placeholder='$t("auth:fields.emailUser")'
  108. //- )
  109. //- v-text-field.mt-2(
  110. //- solo
  111. //- flat
  112. //- prepend-icon='mdi-textbox-password'
  113. //- background-color='grey lighten-4'
  114. //- hide-details
  115. //- ref='iptPassword'
  116. //- v-model='password'
  117. //- :append-icon='hidePassword ? "mdi-eye-off" : "mdi-eye"'
  118. //- @click:append='() => (hidePassword = !hidePassword)'
  119. //- :type='hidePassword ? "password" : "text"'
  120. //- :placeholder='$t("auth:fields.password")'
  121. //- @keyup.enter='login'
  122. //- )
  123. //- template(v-else-if='screen === "tfa"')
  124. //- .body-2 Enter the security code generated from your trusted device:
  125. //- v-text-field.centered.mt-2(
  126. //- solo
  127. //- flat
  128. //- background-color='grey lighten-4'
  129. //- hide-details
  130. //- ref='iptTFA'
  131. //- v-model='securityCode'
  132. //- :placeholder='$t("auth:tfa.placeholder")'
  133. //- @keyup.enter='verifySecurityCode'
  134. //- )
  135. //- template(v-else-if='screen === "changePwd"')
  136. //- .body-2 {{$t('auth:changePwd.instructions')}}
  137. //- v-text-field.mt-2(
  138. //- type='password'
  139. //- solo
  140. //- flat
  141. //- background-color='grey lighten-4'
  142. //- hide-details
  143. //- ref='iptNewPassword'
  144. //- v-model='newPassword'
  145. //- :placeholder='$t(`auth:changePwd.newPasswordPlaceholder`)'
  146. //- )
  147. //- v-text-field.mt-2(
  148. //- type='password'
  149. //- solo
  150. //- flat
  151. //- background-color='grey lighten-4'
  152. //- hide-details
  153. //- v-model='newPasswordVerify'
  154. //- :placeholder='$t(`auth:changePwd.newPasswordVerifyPlaceholder`)'
  155. //- @keyup.enter='changePassword'
  156. //- )
  157. //- template(v-else-if='screen === "forgot"')
  158. //- .body-2 {{ $t('auth:forgotPasswordSubtitle') }}
  159. //- v-text-field.mt-3(
  160. //- solo
  161. //- flat
  162. //- prepend-icon='mdi-email'
  163. //- background-color='grey lighten-4'
  164. //- hide-details
  165. //- ref='iptEmailForgot'
  166. //- v-model='username'
  167. //- :placeholder='$t("auth:fields.email")'
  168. //- )
  169. //- v-card-actions.pb-4
  170. //- v-spacer
  171. //- v-btn(
  172. //- width='100%'
  173. //- max-width='250px'
  174. //- v-if='screen === "login"'
  175. //- large
  176. //- color='primary'
  177. //- dark
  178. //- @click='login'
  179. //- rounded
  180. //- :loading='isLoading'
  181. //- ) {{ $t('auth:actions.login') }}
  182. //- v-btn(
  183. //- width='100%'
  184. //- max-width='250px'
  185. //- v-else-if='screen === "tfa"'
  186. //- large
  187. //- color='primary'
  188. //- dark
  189. //- @click='verifySecurityCode'
  190. //- rounded
  191. //- :loading='isLoading'
  192. //- ) {{ $t('auth:tfa.verifyToken') }}
  193. //- v-btn(
  194. //- width='100%'
  195. //- max-width='250px'
  196. //- v-else-if='screen === "changePwd"'
  197. //- large
  198. //- color='primary'
  199. //- dark
  200. //- @click='changePassword'
  201. //- rounded
  202. //- :loading='isLoading'
  203. //- ) {{ $t('auth:changePwd.proceed') }}
  204. //- v-btn(
  205. //- width='100%'
  206. //- max-width='250px'
  207. //- v-else-if='screen === "forgot"'
  208. //- large
  209. //- color='primary'
  210. //- dark
  211. //- @click='forgotPasswordSubmit'
  212. //- rounded
  213. //- :loading='isLoading'
  214. //- ) {{ $t('auth:sendResetPassword') }}
  215. //- v-spacer
  216. //- v-card-actions.pb-3(v-if='screen === "login" && selectedStrategy.key === "local"')
  217. //- v-spacer
  218. //- a.caption(@click.stop.prevent='forgotPassword', href='#forgot') {{ $t('auth:forgotPasswordLink') }}
  219. //- v-spacer
  220. //- v-card-actions.pb-3(v-else-if='screen === "forgot"')
  221. //- v-spacer
  222. //- a.caption(@click.stop.prevent='screen = `login`', href='#cancelforgot') {{ $t('auth:forgotPasswordCancel') }}
  223. //- v-spacer
  224. loader(v-model='isLoading', :color='loaderColor', :title='loaderTitle', :subtitle='$t(`auth:pleaseWait`)')
  225. notify
  226. </template>
  227. <script>
  228. /* global siteConfig */
  229. // <span>Photo by <a href="https://unsplash.com/@isaacquesada?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Isaac Quesada</a> on <a href="/t/textures-patterns?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>
  230. import _ from 'lodash'
  231. import Cookies from 'js-cookie'
  232. import gql from 'graphql-tag'
  233. import { sync } from 'vuex-pathify'
  234. export default {
  235. i18nOptions: { namespaces: 'auth' },
  236. data () {
  237. return {
  238. error: false,
  239. strategies: [],
  240. selectedStrategyKey: 'local',
  241. selectedStrategy: { key: 'local', strategy: { useForm: true } },
  242. screen: 'login',
  243. username: '',
  244. password: '',
  245. hidePassword: true,
  246. securityCode: '',
  247. continuationToken: '',
  248. isLoading: false,
  249. loaderColor: 'grey darken-4',
  250. loaderTitle: 'Working...',
  251. isShown: false,
  252. newPassword: '',
  253. newPasswordVerify: ''
  254. }
  255. },
  256. computed: {
  257. activeModal: sync('editor/activeModal'),
  258. siteTitle () {
  259. return siteConfig.title
  260. },
  261. isSocialShown () {
  262. return this.strategies.length > 1
  263. },
  264. logoUrl () { return siteConfig.logoUrl }
  265. },
  266. watch: {
  267. strategies(newValue, oldValue) {
  268. this.selectedStrategy = _.head(newValue)
  269. },
  270. selectedStrategyKey (newValue, oldValue) {
  271. this.selectedStrategy = _.find(this.strategies, ['key', newValue])
  272. this.screen = 'login'
  273. if (!this.selectedStrategy.strategy.useForm) {
  274. this.isLoading = true
  275. window.location.assign('/login/' + newValue)
  276. } else {
  277. this.$nextTick(() => {
  278. this.$refs.iptEmail.focus()
  279. })
  280. }
  281. }
  282. },
  283. mounted () {
  284. this.isShown = true
  285. this.$nextTick(() => {
  286. // this.$refs.iptEmail.focus()
  287. })
  288. },
  289. methods: {
  290. /**
  291. * LOGIN
  292. */
  293. async login () {
  294. if (this.username.length < 2) {
  295. this.$store.commit('showNotification', {
  296. style: 'red',
  297. message: this.$t('auth:invalidEmailUsername'),
  298. icon: 'alert'
  299. })
  300. this.$refs.iptEmail.focus()
  301. } else if (this.password.length < 2) {
  302. this.$store.commit('showNotification', {
  303. style: 'red',
  304. message: this.$t('auth:invalidPassword'),
  305. icon: 'alert'
  306. })
  307. this.$refs.iptPassword.focus()
  308. } else {
  309. this.loaderColor = 'grey darken-4'
  310. this.loaderTitle = this.$t('auth:signingIn')
  311. this.isLoading = true
  312. try {
  313. let resp = await this.$apollo.mutate({
  314. mutation: gql`
  315. mutation($username: String!, $password: String!, $strategy: String!) {
  316. authentication {
  317. login(username: $username, password: $password, strategy: $strategy) {
  318. responseResult {
  319. succeeded
  320. errorCode
  321. slug
  322. message
  323. }
  324. jwt
  325. mustChangePwd
  326. mustProvideTFA
  327. continuationToken
  328. }
  329. }
  330. }
  331. `,
  332. variables: {
  333. username: this.username,
  334. password: this.password,
  335. strategy: this.selectedStrategy.key
  336. }
  337. })
  338. if (_.has(resp, 'data.authentication.login')) {
  339. let respObj = _.get(resp, 'data.authentication.login', {})
  340. if (respObj.responseResult.succeeded === true) {
  341. this.continuationToken = respObj.continuationToken
  342. if (respObj.mustChangePwd === true) {
  343. this.screen = 'changePwd'
  344. this.$nextTick(() => {
  345. this.$refs.iptNewPassword.focus()
  346. })
  347. this.isLoading = false
  348. } else if (respObj.mustProvideTFA === true) {
  349. this.screen = 'tfa'
  350. this.securityCode = ''
  351. this.$nextTick(() => {
  352. this.$refs.iptTFA.focus()
  353. })
  354. this.isLoading = false
  355. } else {
  356. this.loaderColor = 'green darken-1'
  357. this.loaderTitle = this.$t('auth:loginSuccess')
  358. Cookies.set('jwt', respObj.jwt, { expires: 365 })
  359. _.delay(() => {
  360. const loginRedirect = Cookies.get('loginRedirect')
  361. if (loginRedirect) {
  362. Cookies.remove('loginRedirect')
  363. window.location.replace(loginRedirect)
  364. } else {
  365. window.location.replace('/')
  366. }
  367. }, 1000)
  368. }
  369. } else {
  370. throw new Error(respObj.responseResult.message)
  371. }
  372. } else {
  373. throw new Error(this.$t('auth:genericError'))
  374. }
  375. } catch (err) {
  376. console.error(err)
  377. this.$store.commit('showNotification', {
  378. style: 'red',
  379. message: err.message,
  380. icon: 'alert'
  381. })
  382. this.isLoading = false
  383. }
  384. }
  385. },
  386. /**
  387. * VERIFY TFA CODE
  388. */
  389. verifySecurityCode () {
  390. if (this.securityCode.length !== 6) {
  391. this.$store.commit('showNotification', {
  392. style: 'red',
  393. message: 'Enter a valid security code.',
  394. icon: 'warning'
  395. })
  396. this.$refs.iptTFA.focus()
  397. } else {
  398. this.isLoading = true
  399. this.$apollo.mutate({
  400. mutation: gql`
  401. {
  402. }
  403. `,
  404. variables: {
  405. continuationToken: this.continuationToken,
  406. securityCode: this.securityCode
  407. }
  408. }).then(resp => {
  409. if (_.has(resp, 'data.authentication.loginTFA')) {
  410. let respObj = _.get(resp, 'data.authentication.loginTFA', {})
  411. if (respObj.responseResult.succeeded === true) {
  412. this.$store.commit('showNotification', {
  413. message: 'Login successful!',
  414. style: 'success',
  415. icon: 'check'
  416. })
  417. _.delay(() => {
  418. window.location.replace('/') // TEMPORARY - USE RETURNURL
  419. }, 1000)
  420. this.isLoading = false
  421. } else {
  422. throw new Error(respObj.responseResult.message)
  423. }
  424. } else {
  425. throw new Error(this.$t('auth:genericError'))
  426. }
  427. }).catch(err => {
  428. console.error(err)
  429. this.$store.commit('showNotification', {
  430. style: 'red',
  431. message: err.message,
  432. icon: 'alert'
  433. })
  434. this.isLoading = false
  435. })
  436. }
  437. },
  438. /**
  439. * CHANGE PASSWORD
  440. */
  441. async changePassword () {
  442. this.loaderColor = 'grey darken-4'
  443. this.loaderTitle = this.$t('auth:changePwd.loading')
  444. this.isLoading = true
  445. const resp = await this.$apollo.mutate({
  446. mutation: gql`
  447. {
  448. }
  449. `,
  450. variables: {
  451. continuationToken: this.continuationToken,
  452. newPassword: this.newPassword
  453. }
  454. })
  455. if (_.get(resp, 'data.authentication.loginChangePassword.responseResult.succeeded', false) === true) {
  456. this.loaderColor = 'green darken-1'
  457. this.loaderTitle = this.$t('auth:loginSuccess')
  458. Cookies.set('jwt', _.get(resp, 'data.authentication.loginChangePassword.jwt', ''), { expires: 365 })
  459. _.delay(() => {
  460. window.location.replace('/') // TEMPORARY - USE RETURNURL
  461. }, 1000)
  462. } else {
  463. this.$store.commit('showNotification', {
  464. style: 'red',
  465. message: _.get(resp, 'data.authentication.loginChangePassword.responseResult.message', false),
  466. icon: 'alert'
  467. })
  468. this.isLoading = false
  469. }
  470. },
  471. /**
  472. * SWITCH TO FORGOT PASSWORD SCREEN
  473. */
  474. forgotPassword () {
  475. this.screen = 'forgot'
  476. this.$nextTick(() => {
  477. this.$refs.iptEmailForgot.focus()
  478. })
  479. },
  480. /**
  481. * FORGOT PASSWORD SUBMIT
  482. */
  483. async forgotPasswordSubmit () {
  484. this.$store.commit('showNotification', {
  485. style: 'pink',
  486. message: 'Coming soon!',
  487. icon: 'ferry'
  488. })
  489. }
  490. },
  491. apollo: {
  492. strategies: {
  493. query: gql`
  494. {
  495. authentication {
  496. activeStrategies {
  497. key
  498. strategy {
  499. key
  500. logo
  501. color
  502. icon
  503. useForm
  504. }
  505. displayName
  506. order
  507. selfRegistration
  508. }
  509. }
  510. }
  511. `,
  512. update: (data) => _.sortBy(data.authentication.activeStrategies, ['order']),
  513. watchLoading (isLoading) {
  514. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'login-strategies-refresh')
  515. }
  516. }
  517. }
  518. }
  519. </script>
  520. <style lang="scss">
  521. .login {
  522. background-image: url('/_assets/img/splash/1.jpg');
  523. background-size: cover;
  524. background-position: center center;
  525. width: 100%;
  526. height: 100%;
  527. &-sd {
  528. background-color: rgba(255,255,255,.8);
  529. backdrop-filter: blur(10px);
  530. -webkit-backdrop-filter: blur(10px);
  531. border-left: 1px solid rgba(255,255,255,.85);
  532. border-right: 1px solid rgba(255,255,255,.85);
  533. width: 450px;
  534. height: 100%;
  535. margin-left: 5vw;
  536. @at-root .no-backdropfilter & {
  537. background-color: rgba(255,255,255,.95);
  538. }
  539. @include until($tablet) {
  540. margin-left: 0;
  541. width: 100%;
  542. }
  543. }
  544. &-logo {
  545. padding: 12px 0 0 12px;
  546. width: 58px;
  547. height: 58px;
  548. background-color: #222;
  549. margin-left: 12px;
  550. border-bottom-left-radius: 7px;
  551. border-bottom-right-radius: 7px;
  552. }
  553. &-title {
  554. height: 58px;
  555. padding-left: 12px;
  556. display: flex;
  557. align-items: center;
  558. text-shadow: .5px .5px #FFF;
  559. }
  560. &-subtitle {
  561. padding: 24px 12px 12px 12px;
  562. color: #111;
  563. font-weight: 500;
  564. text-shadow: 1px 1px rgba(255,255,255,.5);
  565. background-image: linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,.15));
  566. text-align: center;
  567. border-bottom: 1px solid rgba(0,0,0,.3);
  568. }
  569. &-list {
  570. border-top: 1px solid rgba(255,255,255,.85);
  571. padding: 12px;
  572. }
  573. &-form {
  574. padding: 12px;
  575. border-top: 1px solid rgba(255,255,255,.85);
  576. }
  577. &-main {
  578. flex: 1 0 100vw;
  579. height: 100vh;
  580. }
  581. }
  582. </style>