如何通过post to rest服务在feathersjs中发送auth令牌?

时间:2016-09-06 04:43:07

标签: feathersjs

我正在学习羽毛,我正在尝试将一些数据发送到我创建的服务。我在没有任何授权的情况下使用它时工作正常。当我添加授权时,我可以使用邮递员手动发送JWT令牌。但是,当我发送帖子时,我不知道如何在标题中发送令牌或处理此问题的最佳方法。我找到的例子使用socket.io。有没有办法用一个简单的帖子来做到这一点?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=0">
  <title>Feathers Chat</title>
  <link rel="shortcut icon" href="favicon.ico">
  <link rel="stylesheet" href="//cdn.rawgit.com/feathersjs/feathers-chat/v0.1.0/public/base.css">
  <link rel="stylesheet" href="//cdn.rawgit.com/feathersjs/feathers-chat/v0.1.0/public/chat.css">
</head>
<body>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="//unpkg.com/feathers-client@^1.0.0/dist/feathers.js"></script>
<script type="text/javascript">
  var host = 'http://localhost:3030';
  // Set up Feathers client side
  var app = feathers()
    .configure(feathers.rest(host).jquery(jQuery))
    .configure(feathers.hooks())
    .configure(feathers.authentication({ storage: window.localStorage }));
  // authenticate using your JWT that was passed in the short lived cookie
  app.authenticate().then(function(result){
    console.log('Authenticated!', result);
    alert('Your JWT is: ' + app.get('token'));
  }).catch(function(error){
    console.error('Error authenticating!', error);
  });
</script>

<main class="login container">
  <div class="row">
    <div class="col-12 col-6-tablet push-3-tablet text-center">
      <h1 class="font-100">Post</h1>
    </div>
  </div>
  <div class="row">
    <div class="col-12 col-6-tablet push-3-tablet col-4-desktop push-4-desktop text-center">
      <form class="form" method="post" action="/posts">
        <fieldset>
          <input class="block" type="text" name="title" placeholder="title">
        </fieldset>
        <fieldset>
          <input class="block" type="text" name="description" placeholder="description">
        </fieldset>
        <button type="submit" class="button button-primary block login">
          Post
        </button>
      </form>
    </div>
  </div>
</main>
</body>
</html>

感谢您的帮助!到目前为止我真的很喜欢羽毛。

4 个答案:

答案 0 :(得分:1)

好的,这就是我所做的,似乎工作得很好。我不确定羽毛在创建后是否以某种方式自动处理身份验证令牌。一旦我设置帖子通过jquery发送并设置授权标题,它工作正常。谢谢你的帮助。到目前为止,我确实喜欢羽毛!

  $(document).ready(function() {
    $( ".test-form" ).submit(function( event ) {
var token =  app.get('token');

    event.preventDefault();

    $.ajax({
      url: 'http://localhost:3030/posts/',
      type: 'post',
      data: {
      title: $("#title").val(),
        description: $("#description").val()
      },
      headers: {
        Authorization: token
      },
      dataType: 'json',
      success: function (data) {
        console.info(data);
      }
    });
  });});

答案 1 :(得分:1)

使用Feathers客户端。您使用相同的app进行身份验证。

// Get the Posts service to work with
var postsService = app.service('posts');

// Create a post
postsService.create({
    title: $('#title').val(),
    description: $('#description').val()
});

// Do something when a Post is created
postsService.on('created', function (post) {
    // `post` is the newly created post
    // this callback will run whenever a post is created
    console.log(post);
});

您甚至可以在事件处理程序中使用postsService.create方法,例如......

$('form').on('submit', function () {
    postsService.create({
        title: $('#title').val(),
        description: $('#description').val()
    });
});

答案 2 :(得分:0)

阅读本节:http://docs.feathersjs.com/authentication/client.html

您可以使用app.get('token')获取令牌。

答案 3 :(得分:0)

发布数据时,请在授权 - &gt;中设置标题字段您登录时获得的令牌

enter image description here