Mitchell Yuen
Mitchell Yuen

Reputation: 335

Angular/Node authenticate User

I have a node server that is running. and it sends a post to a Twitter Timeline, which I have an Ionic/Angular Application the consumes the Node Server and sends the message.

however, the problem is that in the node server my user's Twitter account info is hard coded and I would like to know how I can send the user's details that I get from the Twitter connect Plugin.

here is my node sever

const express = require('express');
const Twitter = require('twit');

const app = express();
const client = new Twitter({
  consumer_key:         '...',
  consumer_secret:      '...',
  access_token:         '...',
  access_token_secret:  '...',
});


app.use(require('cors')());
app.use(require('body-parser').json());

app.post('/post_tweet', (req, res) => {

  tweet = req.body;

  client
    .post(`statuses/update`, tweet)
    .then(tweeting => {
      console.log(tweeting);

      res.send(tweeting);
    })

   .catch(error => {
    res.send(error);
  });


});

app.listen(3000, () => console.log('Server running'));

twitter service in my angular/ionic app

export class TwitterserviceService {
  api_url = 'http://localhost:3000';

  constructor(private http: HttpClient) { } 

  tweet(tweetdata: string) {
    return this.http.post<any>(`${this.api_url}/post_tweet/`, {status: tweetdata})
        .pipe(map(tweet => {

            alert("tweet posted")

            return tweet;
        }));
}
}

and here is my angular Code that sends a tweet to the node server

sendTweet() {  
  this.api.tweet('sent from phone')
            .pipe(first())
            .subscribe(
                data => {
                    console.log('yes')
                },
                error => {
                  'failed'
                });
}

connect plugin

twitterLogin() {
    this.twitter.login().then((res: any) => {
      localStorage.setItem('twitterLogin', "true");
      this.firebaseService.twitterDetail(this.userId, res);


      this.twitterUser = true;
      if(this.twitterUser == true){
        this.twitter.showUser()
        .then(user => {
          console.log("User: " + user +
                        'username ' + res.username+ " " + user.username);

        });
      }


    }, (err: any) => {
      alert('error: ' + err);
    });
  }

Upvotes: 6

Views: 348

Answers (1)

lousybear
lousybear

Reputation: 451

You will need to set params to AuthenticationHeaders while calling the API service which will implement Interceptor interface in Angular.

You can use this for reference HttpRequest with Interceptor in Angular

Upvotes: 1

Related Questions