Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code to login and logout with accounts.topcoder.com #1

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions .tavis.yml → .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ deploy:
local_dir: dist
on:
branch: qa
- provider: s3
cache_control: private, no-store, no-cache, must-revalidate, max-age=0
detect_encoding: true
access_key_id: $MASTER_AWS_KEY
secret_access_key: $MASTER_AWS_SECRET
bucket: sample.topcoder.com
skip_cleanup: true
local_dir: dist
on:
branch: master
#- provider: s3
# cache_control: private, no-store, no-cache, must-revalidate, max-age=0
# detect_encoding: true
# access_key_id: $MASTER_AWS_KEY
# secret_access_key: $MASTER_AWS_SECRET
# bucket: sample.topcoder.com
# skip_cleanup: true
# local_dir: dist
# on:
# branch: master
env:
matrix:
- CXX=g++-4.8
Expand All @@ -45,7 +45,10 @@ env:
- secure: "vmS6JnYZ9210zdnr/gtobE6VnxQuVWAASNMrhIXyzZfU26gELBN18tgWczESAPLex/gkUKDrmv7IhUvxIIeCH1IznwPmlZXsgCm4IGfMYy7+Fxe+UxbHuJ5oyM4rfxAdp5lYdNMbcopY4F3e7H95SleC+2PdFuuTrWuvHIm8CzaqPV/NndTtdiO+iVkO9usgiOywZuV6Be13PKP4K06JwiNrKbiGRxkVZyVJLlqMPaLUaA/gO7+U3pm/xpRZxxlzVHdAq/QzHzQFdZhijfzjPvcX7qwP2PYfjTxpm47okICldtEZ7v1yWQvlfoQYWGnBZEPgUez0nvk7T3QmQ7dlkH1bw+oiosrLT4cmHd2N3B0n/N33pSu5600DULSK011p+wL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CUh8zNVQjjZhVGeZP0KBw8pwkcxLccrpBr4XbjJQB3O+KIHbSahZHFuQpTbNV1YGLjOfazEW6icUenLS2+4HVsEwTI2I9Ns+Zzr6OXt13K4Zz0MgVFdn2zqYgRR4RzAgMk8sMcgUt5oQqfwHFjv/XVX6dg/44GBW4XLOX1VZblgZfcUCeo0UwA9PE7DOeyaWJrdzNoH/BtW4UMyKkHsBWDA2ck/7EQCgkgoBJ46/tirCoj6CzVG9QxTqJPj2Mo="
addons:
apt:
sources:
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# sample-app
# sample-app

## build & run
```
nvm use
npm install
npm run dev
```

## sample: login & logout
**local**
```
http://localhost:3100/
```
**dev**
```
https://sample.topcoder-dev.com/
```

## sample: reset-password
**local**
```
http://localhost:3100/reset-password?token=xxxxxx&handle=xxxxxx
```
**dev**
```
https://sample.topcoder-dev.com/reset-password?token=xxxxxx&handle=xxxxxx
```
26 changes: 26 additions & 0 deletions app/auth/auth.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

require('angular-jwt')

import { configureConnector, getFreshToken } from 'tc-accounts'

configureConnector({
connectorUrl: process.env.CONNECTOR_URL,
frameId: 'tc-accounts-iframe'
})

const dependencies = ['angular-jwt']

const config = function($httpProvider, jwtInterceptorProvider) {
function jwtInterceptor() {
return getFreshToken()
}

jwtInterceptorProvider.tokenGetter = jwtInterceptor

$httpProvider.interceptors.push('jwtInterceptor')
}

config.$inject = ['$httpProvider', 'jwtInterceptorProvider']

angular.module('appirio-tech-ng-auth', dependencies).config(config)
2 changes: 1 addition & 1 deletion app/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ __webpack_public_path__ = publicPath

require 'angular'
require 'angular-ui-router'
require 'angular-cookies'

require './sample.module'

require 'appirio-tech-ng-api-services'
require 'appirio-tech-ng-auth'

requireContextFiles = (files) ->
Expand Down
33 changes: 33 additions & 0 deletions app/logout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
</head>
<body>
Logging out..
<script type="text/javascript">
function deleteCookie(cookie) {
console.log('Deleting '+cookie+' from Cookie.');
var domain = location.hostname.substring(location.hostname.indexOf('.'));
document.cookie = cookie + "=; path=/; domain=" + domain + "; expires=" + (new Date()).toGMTString()+"; ";
}
function deleteItem(key) {
if(localStorage) {
console.log('Deleting '+key+' from Storage.');
localStorage.removeItem(key);
}
}
if(localStorage) {
// tc
deleteItem('appiriojwt');
deleteCookie('tcjwt');
deleteCookie('tcsso');
// connect
deleteItem('auth0Jwt');
deleteItem('userJWTToken');
deleteItem('userRefreshJWTToken');
deleteItem('v2SSOToken');
}
</script>
</body>
</html>
28 changes: 22 additions & 6 deletions app/sample.module.coffee
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
'use strict'

# Module
dependencies = [
'ui.router'
'ngCookies'
'appirio-tech-ng-auth'
]

angular.module 'sample', dependencies


# Configuration
config = ($locationProvider, $stateProvider) ->
states = {}

$locationProvider.html5Mode true

# customer routes

# Root
states['home'] =
url : '/'
title : 'Home'
controller : 'HomeController as vm'
template : require('./views/home')()

# Logout (TODO: This does not work with the accounts site)
states['logout'] =
url : '/logout'
controller : 'LogoutController'
controller : 'LogoutController as vm'

states['resetPassword'] =
url : '/reset-password?handle&token'
controller : 'ResetPasswordController as vm'
template : require('./views/reset-password')()

for key, state of states
$stateProvider.state key, state
Expand All @@ -33,11 +42,18 @@ config.$inject = ['$locationProvider', '$stateProvider']
angular.module('sample').config config


# Constants
# see webpack.config.js
constants =
LOGIN_RETURN_URL: process.env.LOGIN_RETURN_URL
LOGOUT_RETURN_URL: process.env.LOGOUT_RETURN_URL
ACCOUNTS_LOGIN_URL: process.env.ACCOUNTS_LOGIN_URL
ACCOUNTS_LOGOUT_URL:process.env.ACCOUNTS_LOGOUT_URL
LOGIN_RETURN_URL : process.env.LOGIN_RETURN_URL
LOGOUT_RETURN_URL : process.env.LOGOUT_RETURN_URL
ACCOUNTS_LOGIN_URL : process.env.ACCOUNTS_LOGIN_URL
ACCOUNTS_LOGOUT_URL : process.env.ACCOUNTS_LOGOUT_URL
COOKIE_SPEC :
create: () ->
path : '/'
domain : process.env.COOKIE_DOAMIN
expires: new Date((new Date()).getTime() + (7776000 * 1000))

angular.module('sample').constant 'constants', constants

51 changes: 0 additions & 51 deletions app/scripts/home.coffee

This file was deleted.

76 changes: 76 additions & 0 deletions app/scripts/home.controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

require('../auth/auth.module.js')

{ decodeToken, getFreshToken, logout } = require 'tc-accounts'


HomeController = (
$scope
$log
$window
$location
$stateParams
constants) ->

vm = this

# cehck if user is logged in
vm.isLoggedIn = false

# redirect to accounts.topcoder.com to log in to the system.
# with specifying the callback url on 'retUrl' and 'app' parameters.
# e.g. https://accounts.topcoder-dev.com/login?retUrl=https%3A%2F%2Fsample.topcoder-dev.com%2F
vm.login = (app) ->
accountsUrl = constants.ACCOUNTS_LOGIN_URL + '?app=' + app + '&retUrl=' + encodeURIComponent(constants.LOGIN_RETURN_URL)
$log.info 'redirect to ' + accountsUrl
$window.location = accountsUrl

# redirect to accounts.topcoder.com to log out from the system.
# with specifying the callback url on 'retUrl' parameter.
# e.g. https://accounts.topcoder-dev.com/logout?retUrl=https%3A%2F%2Fsample.topcoder-dev.com%2F
vm.logout = ->
logout().then () ->
$scope.$apply ->
vm.isLoggedIn = false
vm.username = ''
###
AuthService.logout()
accountsUrl = constants.ACCOUNTS_LOGOUT_URL + '?retUrl=' + encodeURIComponent(constants.LOGOUT_RETURN_URL)
$log.info 'redirect to ' + accountsUrl
$window.location = accountsUrl
###

# handle callback
# https://sample.topcoder-dev.com/?jwt=.....
# - jwt : v3 jwt
# - tcjwt: v2 jwt
# - tcsso: sso token
init = ->
getFreshToken().then (token) ->
if token
$scope.$apply ->
vm.isLoggedIn = true
vm.username = decodeToken(token).handle

# if $stateParams.jwt
# TokenService.setAppirioJWT $stateParams.jwt
# # username in jwt
# if AuthService.isLoggedIn()
# vm.username = TokenService.decodeToken().handle

vm

init()


HomeController.$inject = [
'$scope'
'$log'
'$window'
'$location'
'$stateParams'
'constants'
]

angular.module('sample').controller 'HomeController', HomeController
7 changes: 6 additions & 1 deletion app/scripts/logout.controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ LogoutController = (
$rootScope
$location
$state
$cookies
constants
AuthService) ->

vm = this

init = ->
$log.info "****** LOGGED OUT ******"
AuthService.logout()
$cookies.remove 'tcjwt', constants.COOKIE_SPEC.create()
$cookies.remove 'tcsso', constants.COOKIE_SPEC.create()
$state.go 'home'
vm

Expand All @@ -22,6 +25,8 @@ LogoutController.$inject = [
'$rootScope'
'$location'
'$state'
'$cookies'
'constants'
'AuthService'
]

Expand Down
Loading