Api key authentication in Ionic and Ci Rest server

I am developing application using ionic framework and Codeigniter Restful server. I would like to use api key for authentication and I would like to send api key in http authentication header. I would like to know is,

If A user access application for the first time we will authenticate using their username and password and send api key to user and store in client application.

But Api authentication of CI rest server check the authentication header firstly. If no key found and deny the request. It is not very well for first time for login request. How should I solve this.

I send authentication header as follow in order to send in every request. I am new to ionic frame. Please help me how should I control this.

.run(function($ionicPlatform,$http) {
    $http.defaults.headers.common['X-API-KEY'] = 'abcdefghijklmnopqrstuvwxyzabcdabcdeabcde';//here value of stored apikey, it will be empty on login in request