login.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. authentication {
  403. activeStrategies {
  404. key
  405. }
  406. }
  407. }
  408. `,
  409. variables: {
  410. continuationToken: this.continuationToken,
  411. securityCode: this.securityCode
  412. }
  413. }).then(resp => {
  414. if (_.has(resp, 'data.authentication.loginTFA')) {
  415. let respObj = _.get(resp, 'data.authentication.loginTFA', {})
  416. if (respObj.responseResult.succeeded === true) {
  417. this.$store.commit('showNotification', {
  418. message: 'Login successful!',
  419. style: 'success',
  420. icon: 'check'
  421. })
  422. _.delay(() => {
  423. window.location.replace('/') // TEMPORARY - USE RETURNURL
  424. }, 1000)
  425. this.isLoading = false
  426. } else {
  427. throw new Error(respObj.responseResult.message)
  428. }
  429. } else {
  430. throw new Error(this.$t('auth:genericError'))
  431. }
  432. }).catch(err => {
  433. console.error(err)
  434. this.$store.commit('showNotification', {
  435. style: 'red',
  436. message: err.message,
  437. icon: 'alert'
  438. })
  439. this.isLoading = false
  440. })
  441. }
  442. },
  443. /**
  444. * CHANGE PASSWORD
  445. */
  446. async changePassword () {
  447. this.loaderColor = 'grey darken-4'
  448. this.loaderTitle = this.$t('auth:changePwd.loading')
  449. this.isLoading = true
  450. const resp = await this.$apollo.mutate({
  451. mutation: gql`
  452. {
  453. authentication {
  454. activeStrategies {
  455. key
  456. }
  457. }
  458. }
  459. `,
  460. variables: {
  461. continuationToken: this.continuationToken,
  462. newPassword: this.newPassword
  463. }
  464. })
  465. if (_.get(resp, 'data.authentication.loginChangePassword.responseResult.succeeded', false) === true) {
  466. this.loaderColor = 'green darken-1'
  467. this.loaderTitle = this.$t('auth:loginSuccess')
  468. Cookies.set('jwt', _.get(resp, 'data.authentication.loginChangePassword.jwt', ''), { expires: 365 })
  469. _.delay(() => {
  470. window.location.replace('/') // TEMPORARY - USE RETURNURL
  471. }, 1000)
  472. } else {
  473. this.$store.commit('showNotification', {
  474. style: 'red',
  475. message: _.get(resp, 'data.authentication.loginChangePassword.responseResult.message', false),
  476. icon: 'alert'
  477. })
  478. this.isLoading = false
  479. }
  480. },
  481. /**
  482. * SWITCH TO FORGOT PASSWORD SCREEN
  483. */
  484. forgotPassword () {
  485. this.screen = 'forgot'
  486. this.$nextTick(() => {
  487. this.$refs.iptEmailForgot.focus()
  488. })
  489. },
  490. /**
  491. * FORGOT PASSWORD SUBMIT
  492. */
  493. async forgotPasswordSubmit () {
  494. this.$store.commit('showNotification', {
  495. style: 'pink',
  496. message: 'Coming soon!',
  497. icon: 'ferry'
  498. })
  499. }
  500. },
  501. apollo: {
  502. strategies: {
  503. query: gql`
  504. {
  505. authentication {
  506. activeStrategies {
  507. key
  508. strategy {
  509. key
  510. logo
  511. color
  512. icon
  513. useForm
  514. }
  515. displayName
  516. order
  517. selfRegistration
  518. }
  519. }
  520. }
  521. `,
  522. update: (data) => _.sortBy(data.authentication.activeStrategies, ['order']),
  523. watchLoading (isLoading) {
  524. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'login-strategies-refresh')
  525. }
  526. }
  527. }
  528. }
  529. </script>
  530. <style lang="scss">
  531. .login {
  532. background-image: url('/_assets/img/splash/1.jpg');
  533. background-size: cover;
  534. background-position: center center;
  535. width: 100%;
  536. height: 100%;
  537. &-sd {
  538. background-color: rgba(255,255,255,.8);
  539. backdrop-filter: blur(10px);
  540. -webkit-backdrop-filter: blur(10px);
  541. border-left: 1px solid rgba(255,255,255,.85);
  542. border-right: 1px solid rgba(255,255,255,.85);
  543. width: 450px;
  544. height: 100%;
  545. margin-left: 5vw;
  546. @at-root .no-backdropfilter & {
  547. background-color: rgba(255,255,255,.95);
  548. }
  549. @include until($tablet) {
  550. margin-left: 0;
  551. width: 100%;
  552. }
  553. }
  554. &-logo {
  555. padding: 12px 0 0 12px;
  556. width: 58px;
  557. height: 58px;
  558. background-color: #222;
  559. margin-left: 12px;
  560. border-bottom-left-radius: 7px;
  561. border-bottom-right-radius: 7px;
  562. }
  563. &-title {
  564. height: 58px;
  565. padding-left: 12px;
  566. display: flex;
  567. align-items: center;
  568. text-shadow: .5px .5px #FFF;
  569. }
  570. &-subtitle {
  571. padding: 24px 12px 12px 12px;
  572. color: #111;
  573. font-weight: 500;
  574. text-shadow: 1px 1px rgba(255,255,255,.5);
  575. background-image: linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,.15));
  576. text-align: center;
  577. border-bottom: 1px solid rgba(0,0,0,.3);
  578. }
  579. &-list {
  580. border-top: 1px solid rgba(255,255,255,.85);
  581. padding: 12px;
  582. }
  583. &-form {
  584. padding: 12px;
  585. border-top: 1px solid rgba(255,255,255,.85);
  586. }
  587. &-main {
  588. flex: 1 0 100vw;
  589. height: 100vh;
  590. }
  591. }
  592. </style>