Created
April 27, 2018 21:31
-
-
Save jrosczyk/0edaeb1ba7787ce7086a63c211dbc223 to your computer and use it in GitHub Desktop.
VueJS OAuth popup block fix for Safari and Firefox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <button v-if="githubAuth" class="btn btn-dark ml-auto" type="button" @click="popupFix"> | |
| {{ $t('login_with') }} | |
| <fa :icon="['fab', 'github']"/> | |
| </button> | |
| </template> | |
| <script> | |
| export default { | |
| name: 'LoginWithGithub', | |
| computed: { | |
| githubAuth: () => window.config.githubAuth, | |
| url: () => `/api/oauth/github` | |
| }, | |
| mounted () { | |
| window.addEventListener('message', this.onMessage, false) | |
| }, | |
| beforeDestroy () { | |
| window.removeEventListener('message', this.onMessage) | |
| }, | |
| methods: { | |
| popupFix () { | |
| const newWindow = openWindow('', this.$t('login')) | |
| this.login(newWindow) | |
| }, | |
| async login (newWindow) { | |
| const url = await this.$store.dispatch('auth/fetchOauthUrl', { | |
| provider: 'github' | |
| }) | |
| // openWindow(url, this.$t('login')) | |
| newWindow.location.href = url | |
| }, | |
| /** | |
| * @param {MessageEvent} e | |
| */ | |
| onMessage (e) { | |
| if (e.origin !== window.origin || !e.data.token) { | |
| return | |
| } | |
| this.$store.dispatch('auth/saveToken', { | |
| token: e.data.token | |
| }) | |
| this.$router.push({ name: 'home' }) | |
| } | |
| } | |
| } | |
| /** | |
| * @param {Object} options | |
| * @return {Window} | |
| */ | |
| function openWindow (url, title, options = {}) { | |
| if (typeof url === 'object') { | |
| options = url | |
| url = '' | |
| } | |
| options = { url, title, width: 600, height: 720, ...options } | |
| const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screen.left | |
| const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screen.top | |
| const width = window.innerWidth || document.documentElement.clientWidth || window.screen.width | |
| const height = window.innerHeight || document.documentElement.clientHeight || window.screen.height | |
| options.self = "_self" | |
| options.left = ((width / 2) - (options.width / 2)) + dualScreenLeft | |
| options.top = ((height / 2) - (options.height / 2)) + dualScreenTop | |
| const optionsStr = Object.keys(options).reduce((acc, key) => { | |
| acc.push(`${key}=${options[key]}`) | |
| return acc | |
| }, []).join(',') | |
| const newWindow = window.open(url, title, optionsStr) | |
| if (!newWindow) { | |
| console.log('Please unblock popups in your browser settings to login with Github') | |
| } else if (window.focus) { | |
| newWindow.focus() | |
| } | |
| return newWindow | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment