2014-05-01 2 views
1

에서 인증 나는 내 응용 프로그램의 모든 URL에 ?auth_token=x을 추가 할 수 있습니다 당신이 쿠키를했다거나 세부 사항은 로컬 스토리지에 저장된 것처럼 세션을 복원 요구 사항을 가지고있다.엠버 - 간단한 인증 : URL

나는 지난 몇 시간 동안이를 구현하는 생각할 수있는 모든 방법을 시도했지만 난 아무데도받지 못했습니다. 이것이 가능한가? 어디에서 이러한 기능을 추가해야합니까? 나는 해결책을 찾기 위해 관리로

+0

시도해 봤어? http://emberjs.com/guides/routing/query-params/ – blessenm

+0

예, 모든 것이 초기화되고 전환이 진행/완료되면 제대로 작동하는 것 같습니다. 제 경우에는 인증보다 먼저 처리해야하므로 잘 작동하는 자체 쿼리 매개 변수 처리가 있습니다. 문제는 ember-simple-auth에서 로그인 페이지로 리디렉션을 시도하기 전에 인증을 트리거 할 수있는 위치입니다. –

+0

Ember.SimpleAuth를 사용하는 경우 이미 localStorage에 세션 상태가 저장되어 있습니다. 나는 당신이 여기에서 성취하려는 것을 이해하고 있는지 확신하지 못합니다. localStorage에 유효한 세션이 있으면 세션이 자동으로 복원됩니다. 토큰을 'Authorization' 헤더 대신 URL에 서버에 전달하려면 사용자 정의 권한 부여 프로그램을 구현할 수 있습니다 (여기를 참조하십시오 : https://github.com/simplabs/ember-simple-auth#implementinga-a- 사용자 정의 권한 부여 자). – marcoow

답변

1

는 나는 잘 모르겠어요 얼마나 정확하지만, 여기에 내 자신의 질문에 대답 해요!

application.js :

// Really simple query parameter access 
(function($) { 
    $.QueryString = (function(a) { 
    if (a == "") return {}; 
    var b = {}; 
    for (var i = 0; i < a.length; ++i) 
    { 
     var p=a[i].split('='); 
     if (p.length != 2) continue; 
     b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); 
    } 
    return b; 
    })(window.location.search.substr(1).split('&')) 
})(jQuery); 

초기화/authentication.js.coffee :

LocalStorageWithURLAuth = Ember.SimpleAuth.Stores.LocalStorage.extend 
    init: -> 
    if $.QueryString['auth_token'] 
     auth_token = $.QueryString['auth_token'] 
     Ember.$.ajax(
     async: false 
     url: '/users/sign_in' 
     type: 'POST' 
     data: { auth_token: auth_token } 
     dataType: 'json' 
    ).then (data) => 
     @persist 
      auth_token: data.auth_token 
      auth_email: data.auth_email 
      user_id: data.user_id 
      authenticatorFactory: 'authenticator:devise' 
     @_super() 
     , (error) => 
     @_super() 
    else 
     @_super() 

Ember.Application.initializer 
    name: 'authentication' 
    initialize: (container, application) -> 
    Ember.SimpleAuth.Session.reopen 
     user: (-> 
     userId = @get 'user_id' 
     if !Ember.isEmpty userId 
      container.lookup('store:main').find 'user', userId 
    ).property('user_id') 

    container.register 'session-store:local-storage-url', LocalStorageWithURLAuth 

    Ember.SimpleAuth.setup container, application, 
     storeFactory: 'session-store:local-storage-url' 
     authenticatorFactory: 'authenticator:devise' 
     authorizerFactory: 'authorizer:devise' 
0

나는 당신이 무슨 일을하는지 이해가 확실하지 않다. 쿼리 문자열의 인증 토큰에서 세션을 복원하는 것처럼 보입니까? 실제로 인증 자의 restore 방법이 사용됩니다 (여기의 문서 참조 : http://ember-simple-auth.simplabs.com/ember-simple-auth-devise-api-docs.html#Ember-SimpleAuth-Authenticators-Devise-restore). 또한 응용 프로그램이 시작될 때 쿼리 문자열이 비어 있지 않습니까?

+0

쿼리 문자열에 새 세션을 만드는 데 사용되는 일회성 auth_token 매개 변수가 있습니다. 나는 그것이 최선의 장소라고 생각하면서'restore' 메소드를 작동 시키려고 많은 시간을 보냈지 만, 세션을 복원하고 그 메소드에서 추가적인 ajax 요청을 실행하는 데 필요한 속성의 일부만이 까다로운 것으로 판명되었습니다. . 쿼리 문자열은 전자 메일의 링크 또는 외부 서비스의 리디렉션 체인의 일부로 직접 사용자를 인증하는 데 사용되므로 응용 프로그램이 시작될 때 제공됩니다. –

+0

이제는 생각해 보았습니다.'restore' 메서드로 문제가 발생했을 때 실제로 필요할 때 호출되지 않았다고 생각했기 때문에 거기에 넣은 코드는 쓸모가 없었습니다. 복원 기능을 실행하고 세션을 복원 할 수 있도록 LocalStorage를 미리 채우는 방식으로 접근했습니다. –

+0

'restore' 메소드는 애플리케이션이 시작될 때 그리고 저장소에서 어떤 것이 변경 될 때 호출됩니다. – marcoow