从本机应用程序登录催化剂Web应用程序

时间:2018-06-05 15:35:24

标签: perl authentication react-native login catalyst

我有一个Catalyst网络应用程序,我想知道是否可以从匹配的react-native移动应用程序登录此应用程序? 认证和授权工作已在催化剂应用程序中完成。

由于

编辑1

我尝试发出POST请求,但收到“网络请求失败”。催化剂服务器正在运行。

我使用的反应天然鳕鱼样本

export default class Login extends Component<{}> {
  constructor(props){
    super(props);
    this.state = {
      username: "",
      password:"",
    }
  }

  async _onLoginPressed(){
    try {
      let response = await fetch('http://localhost:port_number/login', {
        method:'POST',
        headers:{
          'Accept':'application/json',
          'Content-Type':'application/json'
        },
        body: JSON.stringify({
            username: this.state.username,
            password: this.state.password,
        })
      });
      let res = await response;
      console.log('response:' + res);
    } catch (err) {
      console.log(err);
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          placeholder="username"
          returnKeyType="next"
          style={styles.input}
          onSubmitEditing={() => this.passwordInput.focus()}
          keyboardType="default"
          autoCorrect={false}
          onChangeText={(value) => this.setState({username: value})}
          value={this.state.username}
          />
          <TextInput
            placeholder="password"
            secureTextEntry={true}
            returnKeyType="go"
            style={styles.input}
            ref={(input) => this.passwordInput = input}
            keyboardType="default"
            autoCapitalize="none"
            autoCorrect={false}
            onChangeText={(value) => this.setState({password: value})}
            value={this.state.password}
            />
          <TouchableOpacity style={styles.buttonContainer} onPress={this._onLoginPressed.bind(this)}>
            <Text style={styles.buttonText}> LOGIN</Text>
          </TouchableOpacity>
      </View>
    );
  }
}

来自催化剂网络应用程序的login.pm控制器:

sub index :Path :Args(0) :FormConfig {
    my ( $self, $c ) = @_;
    my $form = $c->stash->{form};

    # Get the username and password from form
    my $username = $c->request->params->{username};
    my $password = $c->request->params->{password};

    # If the username and password values were found in form
    if ($username && $password) {
        # Attempt to log the user in
        if ($c->authenticate({ username => $username,
                                   password => $password  } )) {
            # If successful, then let them use the application

                $c->response->redirect($c->uri_for(
                  $c->controller('Root')->action_for('index')));
                return;
        #    Set an error message
            #    $c->stash(error_msg => "Renew");
        #}
            } else {
                # Set an error message
                $c->stash(error_msg => "Incorrect parameter(s)");
            }
        } else {
            # Set an error message
            $c->stash(error_msg => "Empty parameter(s)")
                unless ($c->user_exists);
    }
    if ($c->user_exists) {
        if(DateTime->compare($c->user->get('expiration_date'),DateTime->now) == -1) {
            $c->stash(error_msg => "Renew");
        }
    }
    # If either of above don't work out, send to the login page
    $c->stash(template => 'login.tt');
    $c->forward('View::HTML');
}

我不知道是否必须使用auth API。我看到JWT可以生成令牌,但我对不同方面(服务器,网络应用程序,移动应用程序)如何使用这些令牌感到困惑。我是否需要在控制器中编写特定代码来处理这种身份验证?

0 个答案:

没有答案
相关问题